Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Valid code?

by htmanning (Friar)
on Mar 17, 2020 at 21:10 UTC ( [id://11114402]=perlquestion: print w/replies, xml ) Need Help??

htmanning has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I'm brain farting here. Is this >=- valid?
$no_days = "(TO_DAYS(dateadded)-TO_DAYS(Now())>=-$newdateadded)";
I'm trying to track down an error and this looks wrong to me.

Replies are listed 'Best First'.
Re: Valid code?
by LanX (Saint) on Mar 17, 2020 at 21:17 UTC
    > this looks wrong to me.

    It's a string in "doublequotes" how can it be wrong?

    > Is this >=- valid?

    Even if it was eval'ed as Perl code later, it doesn't look wrong to me:

    • >= is a comparison
    • - is a unary negation

    Think -1 >= -4

    Untested.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Valid code?
by kcott (Archbishop) on Mar 17, 2020 at 21:31 UTC

    G'day htmanning,

    "Is this >=- valid?"

    Perl says it is:

    $ perl -ce '$no_days = "(TO_DAYS(dateadded)-TO_DAYS(Now())>=-$newdatea +dded)";' -e syntax OK

    You appear to have added sufficient parentheses to avoid precedence issues:

    $ perl -MO=Deparse,-p -e '$no_days = "(TO_DAYS(dateadded)-TO_DAYS(Now( +))>=-$newdateadded)";' ($no_days = "(TO_DAYS(dateadded)-TO_DAYS(Now())>=-$newdateadded)"); -e syntax OK

    Is the value assigned to $no_days what you were expecting?

    $ perl -e ' $newdateadded = "NDA"; $no_days = "(TO_DAYS(dateadded)-TO_DAYS(Now())>=-$newdateadded)"; print $no_days; ' (TO_DAYS(dateadded)-TO_DAYS(Now())>=-NDA)

    As you've provided no context for the single statement you posted, it's impossible to tell if it's actually doing what you want.

    — Ken

Re: Valid code?
by hippo (Bishop) on Mar 17, 2020 at 21:29 UTC
    $ perl -cw -e '$no_days = "(TO_DAYS(dateadded)-TO_DAYS(Now())>=-$newda +teadded)";' Name "main::newdateadded" used only once: possible typo at -e line 1. Name "main::no_days" used only once: possible typo at -e line 1. -e syntax OK

    Ergo, valid Perl.

Re: Valid code?
by 1nickt (Canon) on Mar 17, 2020 at 22:54 UTC

    Hi, looks wrong to me too.

    I guess you are trying to generate an SQL snippet for MySQL. Assuming $newdateadded is a number, not a date,

    (TO_DAYS(dateadded)-TO_DAYS(Now())>=-$newdateadded)
    or ...
    ( N # number of days from 0 for a date presumably in the past - M # number of days from 0 to now ) # will yield a negative number >= -O # some number of days, but negative
    ...
    so your SQL will be something like:
    ( ( TO_DAYS(dateadded) - TO_DAYS(Now()) ) >= -42 )
    and for values like the first of the month and today, that would be parsed as:
    ( ( TO_DAYS("2020-03-01") - TO_DAYS("2020-03-17") ) >= -42 )
    ... or:
    (737850 - 737866) >= -42)
    ... which evaluates to true, so whatever runs the query captured in the $no_days variable will know that.

    This may be what you want, but I still say it looks wrong, because of the convoluted logic in the comparison, and because of the variable naming.

    Hope this helps!


    The way forward always starts with a minimal test.
      I guess you are trying to generate an SQL snippet for MySQL.

      If that is true, htmanning is probably creating an SQL injection vulnerability here. As always, placeholders should be used instead. Not only does that prevent SQL injections, but it also allows caching and reuse of prepared SQL statements.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Valid code?
by soonix (Canon) on Mar 18, 2020 at 15:43 UTC

    As all the others said, it is syntactically correct, even if you check only the part that is between quotes.

    That said, I am sad that nobody suggested basic arithmetic transformation from (reformatted for better clarity)
    TO_DAYS(dateadded) - TO_DAYS(Now()) >= -$newdateadded
    to
    TO_DAYS(Now()) - TO_DAYS(dateadded) <= $newdateadded
    of course, that is if the formula was right from the beginning. Otherwise the transformed one is as wrong as the original...
Re: Valid code?
by Marshall (Canon) on Mar 19, 2020 at 01:27 UTC
    $no_days = "(TO_DAYS(dateadded)-TO_DAYS(Now())>=-$newdateadded)";
    Without running any code, it appears to me that you have a "units" problem. dateadded and Now are transformed by the TO_DAYS function but their difference is being compared to $newdateadded. Why doesn't TO_DAYS() apply to all 3 terms? Aside from that, this is string, why not:
    $no_days = TO_DAYS($dateadded)-TO_DAYS(Now()) >= TO_DAYS($newdateadded);

    Put your code in a program and run it!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11114402]
Approved by LanX
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (None)
    As of 2024-04-25 00:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found