"anonymous" variable in Perl?

Discussion in 'Perl Misc' started by Ronald Fischer, Nov 30, 2004.

  1. Consider a call to, say, localtime, where I only need the current month:

    my ($a, $b, $c, $d, $month) = localtime time,

    Here I had to invent names ($a...$d) for the first four fields. Is it possible
    in Perl to have kind of an "anonymous" variable as a placeholder for those
    fields, similar to the '_' variable in Prolog?

    I darkly remember to have read about such a feature introduced with Perl 5.6,
    but I can't find out how to do it.

    Ronald
    Ronald Fischer, Nov 30, 2004
    #1
    1. Advertising

  2. On Tue, 30 Nov 2004 01:29:08 -0800, Ronald Fischer wrote:

    > Consider a call to, say, localtime, where I only need the current month:
    >
    > my ($a, $b, $c, $d, $month) = localtime time,
    >
    > Here I had to invent names ($a...$d) for the first four fields. Is it possible
    > in Perl to have kind of an "anonymous" variable as a placeholder for those
    > fields, similar to the '_' variable in Prolog?
    >
    > I darkly remember to have read about such a feature introduced with Perl 5.6,
    > but I can't find out how to do it.
    >
    > Ronald


    You can use undef as an lvalue, as in:

    my $month;
    (undef,undef,undef,undef,$month) = localtime time;

    However, in this case you might be better off using:

    my $month = (localtime)[4];

    which indexes directly in to the return from localtime, which is a list.
    And which defaults to using time() as the time too.

    Rich
    Richard Gration, Nov 30, 2004
    #2
    1. Advertising

  3. Ronald Fischer

    KKramsch Guest

    In <> (Ronald Fischer) writes:

    >Consider a call to, say, localtime, where I only need the current month:


    >my ($a, $b, $c, $d, $month) = localtime time,


    >Here I had to invent names ($a...$d) for the first four fields. Is it possible
    >in Perl to have kind of an "anonymous" variable as a placeholder for those
    >fields, similar to the '_' variable in Prolog?


    You could use

    (undef, undef, undef, undef, my $month) = localtime;

    What I usually do in such cases is something like this

    my $month = (localtime)[4];

    Karl

    --
    Sent from a spam-bucket account; I check it once in a blue moon. If
    you still want to e-mail me, cut out the extension from my address,
    and make the obvious substitutions on what's left.
    KKramsch, Nov 30, 2004
    #3
  4. Ronald Fischer

    J. Romano Guest

    Richard Gration <> wrote in message news:<>...
    >
    > You can use undef as an lvalue, as in:
    >
    > my $month;
    > (undef,undef,undef,undef,$month) = localtime time;



    Just so you know, you can combine the above two lines to one that
    looks like:

    my (undef, undef, undef, undef, $month) = localtime time;

    or even (as has already been pointed out by another poster):

    (undef, undef, undef, undef, my $month) = localtime time;

    I prefer the first line over the second because I find it's more
    readable.

    I wouldn't normally think that an "undef" would be allowed in a
    variable declaration list, but fortunately Perl is smart enough to
    realize that "undef" in a variable declaration list should be ignored.
    (As a result, "undef" occasionally makes a great placeholder.)

    Just thought you might want to know.

    -- Jean-Luc
    J. Romano, Dec 1, 2004
    #4
  5. Ronald Fischer

    Juha Laiho Guest

    KKramsch <> said:
    >In <>
    >(Ronald Fischer) writes:
    >
    >>Consider a call to, say, localtime, where I only need the current month:

    >
    >>my ($a, $b, $c, $d, $month) = localtime time,

    >
    >What I usually do in such cases is something like this
    >
    >my $month = (localtime)[4];


    .... and if more than one of the return values is needed, it's equally simple:

    my ($day,$month) = (localtime)[3,4];
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho, Dec 1, 2004
    #5
  6. Juha Laiho wrote:
    > ... and if more than one of the return values is needed, it's equally
    > simple:
    > my ($day,$month) = (localtime)[3,4];

    Nice, an array slice. Couldn't you also use the range operator to say get
    the values two to five with [2 ..5 ]?

    One quick question, though. Is there an elegant way of getting the time
    components in a hash? It would be easier to memorize if the components had
    human-readable names. I think the indeces are mostly arbitrary apart from
    going from smaller units to larger which somewhat helps.

    --
    With kind regards Veli-Pekka Tätilä ()
    Accessibility, game music, synthesizers and programming:
    http://www.student.oulu.fi/~vtatila/
    Veli-Pekka Tätilä, Aug 4, 2005
    #6
  7. Ronald Fischer

    Anno Siegel Guest

    Veli-Pekka Tätilä <> wrote in comp.lang.perl.misc:
    > Juha Laiho wrote:


    > > ... and if more than one of the return values is needed, it's equally
    > > simple:
    > > my ($day,$month) = (localtime)[3,4];

    > Nice, an array slice. Couldn't you also use the range operator to say get
    > the values two to five with [2 ..5 ]?
    >
    > One quick question, though. Is there an elegant way of getting the time
    > components in a hash? It would be easier to memorize if the components had
    > human-readable names. I think the indeces are mostly arbitrary apart from
    > going from smaller units to larger which somewhat helps.


    You can do it manually,

    my %t;
    @t{ qw( sec, min, hour, mday, mon, year, wday, yday, isdst)} = localtime;
    my ( $day, $month) = @t{ qw( mday mon)};

    but that's probably not what you mean by elegant. The non-standard
    module Time::piece (available from CPAN) does it for you. It overrides
    localtime() and gmtime() so that they return objects which support
    appropriate methods.

    use Time::piece;
    my $t = localtime;
    ( $day, $month) = ($t->mday, $t->mon);

    Note that Time::piece returns the month (and year) in conventional
    counting (from 1) whereas localtime() counts from 0.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Aug 4, 2005
    #7
  8. Anno Siegel wrote:
    <localtime>
    >> One quick question, though. Is there an elegant way of getting the time
    >> components in a hash? It would be easier to memorize <snip>

    > You can do it manually, <<snip>
    > but that's probably not what you mean by elegant.

    Well, no. Having to memorize the ordre at the point of using the function
    sort of negates my point. Because if you memorize the order, it is just
    easier to deal with a list and comment appropriately.

    > module Time::piece (available from CPAN) does it for you. It overrides
    > localtime() and gmtime() so that they return objects

    Ah, that's the ticket. I'm not sure if I'll change to this interface
    full-time but nice to know someone has already done the hard work, as usual
    in Perl.

    > Note that Time::piece returns the month (and year) in conventional
    > counting (from 1)

    Actually, I find this more intuitive.

    --
    With kind regards Veli-Pekka Tätilä ()
    Accessibility, game music, synthesizers and programming:
    http://www.student.oulu.fi/~vtatila/
    Veli-Pekka Tätilä, Aug 4, 2005
    #8
  9. Veli-Pekka Tätilä <> kirjoitti 04.08.2005:
    > Juha Laiho wrote:
    >> ... and if more than one of the return values is needed, it's equally
    >> simple:
    >> my ($day,$month) = (localtime)[3,4];

    > Nice, an array slice. Couldn't you also use the range operator to say get
    > the values two to five with [2 ..5 ]?
    >
    > One quick question, though. Is there an elegant way of getting the time
    > components in a hash? It would be easier to memorize if the components had
    > human-readable names. I think the indeces are mostly arbitrary apart from
    > going from smaller units to larger which somewhat helps.


    For one more way to do it, see the examples section in "perldoc constant".

    --
    Ilmari Karonen
    To reply by e-mail, please replace ".invalid" with ".net" in address.

    "My father used to claim that he heard god on numerous occasions. He
    would ask god questions and keep a record of the answers, and at the end
    of the year he would do a chi-square analysis to find out whether god had
    been right more often than chance would lead one to expect." -- Pat Bowne
    Ilmari Karonen, Aug 4, 2005
    #9
  10. Ronald Fischer

    Peter Scott Guest

    On Thu, 04 Aug 2005 09:04:07 +0000, Anno Siegel wrote:
    > The non-standard
    > module Time::piece (available from CPAN) does it for you. It overrides
    > localtime() and gmtime() so that they return objects which support
    > appropriate methods.
    >
    > use Time::piece;
    > my $t = localtime;
    > ( $day, $month) = ($t->mday, $t->mon);


    Unnecessary to use a non-standard module:

    % perl
    use Time::localtime;
    my ($day, $month) = (localtime->mday, localtime->mon);
    print "$day $month\n";
    ^D
    8 7

    --
    Peter Scott
    http://www.perlmedic.com/
    http://www.perldebugged.com/
    Peter Scott, Aug 8, 2005
    #10
  11. Ronald Fischer

    Anno Siegel Guest

    Peter Scott <> wrote in comp.lang.perl.misc:
    > On Thu, 04 Aug 2005 09:04:07 +0000, Anno Siegel wrote:
    > > The non-standard
    > > module Time::piece (available from CPAN) does it for you. It overrides
    > > localtime() and gmtime() so that they return objects which support
    > > appropriate methods.
    > >
    > > use Time::piece;
    > > my $t = localtime;
    > > ( $day, $month) = ($t->mday, $t->mon);

    >
    > Unnecessary to use a non-standard module:
    >
    > % perl
    > use Time::localtime;
    > my ($day, $month) = (localtime->mday, localtime->mon);
    > print "$day $month\n";
    > ^D
    > 8 7


    Ah, good to know there's an alternative. I like the functionality of
    Time::piece, but not the implementation.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Aug 9, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reporter
    Replies:
    3
    Views:
    465
    Mike Schilling
    May 12, 2007
  2. Replies:
    1
    Views:
    213
  3. Replies:
    29
    Views:
    194
  4. Replies:
    0
    Views:
    77
  5. George Mpouras

    access anonymous variable

    George Mpouras, Feb 1, 2011, in forum: Perl Misc
    Replies:
    8
    Views:
    101
Loading...

Share This Page