Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Date Handling in Perl

by joeymac (Acolyte)
on Jul 11, 2012 at 15:39 UTC ( [id://981167]=note: print w/replies, xml ) Need Help??


in reply to Re: Date Handling in Perl
in thread Date Handling in Perl

Thanks. I have been playing with your method, but I'm not completely sure what this line is doing:

$hour > 10 ? $_=0 : $_=60*60*24; # seconds in the day

It seems to be some pattern matching that I am not completely familiar with. Do you mind elaborating a little? I was trying to tweak it to see if I could make it print today and/or yesterday in the final position of the directory path before I add it to my operational code, but I can only ever get it to print yesterday (/07/10).

Replies are listed 'Best First'.
Re^3: Date Handling in Perl
by frozenwithjoy (Priest) on Jul 11, 2012 at 16:57 UTC
    joeymac: That line is using the ternary conditional operator. It is essentially shorthand for an if-then-else conditional. Take a look here for more info: Conditional Operator.
Re^3: Date Handling in Perl
by Anonymous Monk on Jul 11, 2012 at 16:01 UTC

    The code is better written as:

    $_ = $hour > 10 ? 0 : 60*60*24; # seconds in the day my ($mday,$mon,$year) = (localtime(time-$_))[3..5];

    Using $_ here is just laziness of not declaring a new variable. The first line returns 0 if hour <= 10, and 86400 otherwise. It is then used to modify the time() function to either get today's date (when 0) or yesterday's (when 86400).

    (I do wonder where that corruption of the ternary operator comes from. It makes no sense at all. Unless someone is teaching it as a "shorthand if"...)

Re^3: Date Handling in Perl
by aitap (Curate) on Jul 11, 2012 at 16:06 UTC

    Oh, it looks like some mistake I've done, and I can't understand what's wrong (look at the Re: Date Handling in Perl - it's similar).

    I tried to use Conditional Operator to put the time (in seconds) to subtract in the $_ variable. It can be done the other way: my $subt = 0; $hour > 10 || $subt = 60*60*24; (in this case, you'll need to use $subt instead of $_)

    Sorry if my advice was wrong.

      It's the ternary operator you've botched.

      my $subst; for my $hour (5, 12) { $hour > 10 ? $subst = 0 : $subst = 86400; print $subst, "\n"; } for my $hour (5, 12) { $subst = $hour > 10 ? 0 : 86400; print $subst, "\n"; } ## output 86400 86400 86400 0
        Thanks, it's smaller and was even stated in documentation:

        Because this operator produces an assignable result, using assignments without parentheses will get you in trouble. For example, this:

        $a % 2 ? $a += 10 : $a += 2

        Really means this:

        (($a % 2) ? ($a += 10) : $a) += 2

        I should have noticed it.

        Sorry if my advice was wrong.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://981167]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-29 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found