Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: how can I get localtime without mutating any variables

by thirtySeven (Acolyte)
on Jan 06, 2021 at 20:37 UTC ( [id://11126470]=note: print w/replies, xml ) Need Help??


in reply to Re: how can I get localtime without mutating any variables
in thread how can I get localtime without mutating any variables

no the problem was not with the "$_" variables but rather with the fact that I was changing the $yr $day, etc variables. It seems that using a slice worked though.
  • Comment on Re^2: how can I get localtime without mutating any variables

Replies are listed 'Best First'.
Re^3: how can I get localtime without mutating any variables
by LanX (Saint) on Jan 06, 2021 at 20:45 UTC
    you had the right idea, but better replace $_ with undef

    from perlfunc#my-VARLIST

    Note that with a parenthesised list, undef can be used as a dummy placeholder, for example to skip assignment of initial values:
    my ( undef, $min, $hour ) = localtime;

    you can always use undef on the left-hand-side to ignore assignments.

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

Re^3: how can I get localtime without mutating any variables
by Marshall (Canon) on Jan 07, 2021 at 01:05 UTC
    This reply is hard for me to understand. The whole idea of this is to set the $yr and $day, etc. variables!

    my (undef, $min, $hr, $day, $mon, $year) = localtime();
    and
    my ($min, $hr, $day, $mon, $year) = (localtime())[1..5];
    mean exactly the same thing.

    Perhaps one point of confusion is that your statements like my $year; are completely unnecessary. That creates the variable $year and assigns it the value of undef. In general, combine the creation of the variable and the assignment of useful value to it into one statement. There is no need to have a list of my $year; type of statements before assigning values to them. Also limit the use of these newly created "my" variables to the smallest scope practical.

    In general, $_ , the "default variable", "belongs to Perl" - meaning that is something Perl sets and you read, but you never set or write to yourself. There are of course exceptions to this. However, for your first few hundred programs, you are unlikely to happen across one of these exceptions.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-19 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found