Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: how can I get localtime without mutating any variables

by Fletch (Bishop)
on Jan 06, 2021 at 20:26 UTC ( [id://11126467]=note: print w/replies, xml ) Need Help??


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

Presuming your problem is that you don't want the pieces where you're now using $_ you'd either use undef in those locations, or use a slice and pull out just the ones you're actually interested in; e.g.

(undef, $min, $hr, $day, $mon, $year) = localtime(); ($min, $hr) = (localtime())[1,2];

You may also want to consider DateTime or Time::Piece to provide an object interface instead.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: how can I get localtime without mutating any variables
by thirtySeven (Acolyte) on Jan 06, 2021 at 20:37 UTC
    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.
      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

      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://11126467]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 19:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found