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

Hello I am new to perl.
my $min; my $hr; my $day; my $mon; my $yr; ($_,$min,$hr,$day,$mon,$yr,$_,$_,$_) = localtime();

I have been able to figure out equivalent ways to do the above, but have not figured out how to do it without mutating any variables. I would appreciate some insight.

Replies are listed 'Best First'.
Re: how can I get localtime without mutating any variables
by Fletch (Bishop) on Jan 06, 2021 at 20:26 UTC

    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.

      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.

Re: how can I get localtime without mutating any variables
by hippo (Archbishop) on Jan 06, 2021 at 20:28 UTC

    TIMTOWTDI but a slice is simple and effective.

    #!/usr/bin/env perl use strict; use warnings; my ($min, $hr, $day, $mon, $yr) = (localtime())[1..5]; print "$min $hr $day $mon $yr\n";

    🦛

      thank you this is what I wanted. I am now going to look into what a slice is.
Re: how can I get localtime without mutating any variables
by kcott (Archbishop) on Jan 06, 2021 at 22:41 UTC

    G'day thirtySeven,

    Welcome to the Monastery.

    I see you've indicated that ++hippo's solution is what you want.

    You did say "without mutating any variables"; however, that solution will require mutating variables, e.g.:

    $mon += 1; $yr += 1900;

    See localtime for an explanation of that.

    My local time:

    $ date Thu, 7 Jan 2021 09:03:14

    Values from localtime:

    $ perl -E 'say for (localtime)[1..5]' 3 9 7 0 121

    There is a built-in module, Time::Piece, that will handle those extra calculations for you. See the documentation for a variety of ways to use that module. For a direct comparison with the output from localtime above:

    $ perl -E 'use Time::Piece; my $t = localtime; say $t->$_ for qw{min h +our mday mon year}' 3 9 7 1 2021

    Note that Time::Piece was released with Perl 5.10.0 — see "perl5100delta: New modules" — so you'll need that version or later to use this. If you are working with an older version, I'd recommend updating: the latest stable version is 5.32.0.

    — Ken