Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

List context with map

by abaxaba (Hermit)
on Nov 05, 2004 at 01:10 UTC ( [id://405352]=perlquestion: print w/replies, xml ) Need Help??

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

Some unexpected behavior, with Date::Calc, context, and map. Most of the Date::Calc functions expect a list of @args, which can easily be passed around with arrays (as opposed to separate scalars for each $arg). The function map returns a list. But consider this:
my @eDate=map{$cgi->param($_)}qw(eYear eMonth eDate); @eDate=Add_Delta_Days(@eDate,1);
That works okay, but I should be able to skip an assignment, and go straight through Add_Delta_Days with the map, like so:
my @eDate= Add_Delta_Days(map{$cgi->param($_)} qw(eYear eMonth eDate),1);
Now, to me, that's but a simple replace, replacing @eDate in the original Add_Delta_Days call with my map, which originally defines @eDate. But Date::Calc throws this:

Usage: Date::Calc::Add_Delta_Days(year, month, day, Dd)

Soooo, I tried this:
my @eDate=Add_Delta_Days((map{$cgi->param($_)}qw(eYear eMonth eDate)), +1);
And explictly wrap the map in parens. It worked! Whats the difference, and thanks in advance for shedding the light!

ÅßÅ×ÅßÅ
"It is a very mixed blessing to be brought back from the dead." -- Kurt Vonnegut

Replies are listed 'Best First'.
Re: List context with map
by tachyon (Chancellor) on Nov 05, 2004 at 01:40 UTC

    Whitout the parens you map is *greedy* and thus eats argument 4 ie the digit 1. There is a good chance you have no cgi param '1' so this becomes undef.

    use CGI; my $q = CGI->new( { one => 1, two => 2, three => 3 } ); test( map{$q->param($_)||"${_}_IS_NULL"}qw(one two three) , 4 ); test( ( map{$q->param($_)||"${_}_IS_NULL"}qw(one two three) ), 4 ); sub test { $"=', ';print "Args( @_ )\n" } __DATA__ Args( 1, 2, 3, 4_IS_NULL ) Args( 1, 2, 3, 4 )

    Your obvious expectation is that the qw( ) perfoms a useful binding function to the map when in fact you are getting a flat list: func( map{$q->param($_)} 1, 2, 3, 4 )

    cheers

    tachyon

      map is *greedy* and thus eats argument 4 ie the digit 1. There is a good chance you have no cgi param '1' so this becomes undef.
      perldoc CGI:
      If the parameter does not exist at all, then param() will return undef in a scalar context, and the empty list in a list context.
      so the 1 will not become undef, it will vanish entirely.
•Re: List context with map
by merlyn (Sage) on Nov 05, 2004 at 08:08 UTC
    $cgi->param($_)
    Caution! There's no guarantee that you'll get one output for one input there. You could get zero, or two, or twenty, and that'd really mess up your output. Maybe you want to add scalar in front of that.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      || '' #:-)
Re: List context with map
by VSarkiss (Monsignor) on Nov 05, 2004 at 03:35 UTC

    Brother tachyon has the right answer above. If you'd like to figure out stuff like this for yourself, the B::Deparse module is very handy:

    $ perl -MO=Deparse -e 'Add_Delta_Days( map{$cgi->param($_)} qw(eYear e +Month eDate), 1)' Add_Delta_Days(map({$cgi->param($_);} ('eYear', 'eMonth', 'eDate'), 1) +); -e syntax OK $ $ perl -MO=Deparse -e 'Add_Delta_Days( (map{$cgi->param($_)}qw(eYear e +Month eDate)), 1)' Add_Delta_Days(map({$cgi->param($_);} 'eYear', 'eMonth', 'eDate'), 1); -e syntax OK
    Note where it put the parentheses.

Re: List context with map
by NetWallah (Canon) on Nov 05, 2004 at 03:55 UTC
    FWIW - you could also have written this by explicitly using parens at the boundary of the "map" parameters (my preference):
    my @eDate=Add_Delta_Days(map({$cgi->param($_)}qw(eYear eMonth eDate)), +1);
    Update: Oops - just noticed that VSarkiss already shows this option. Please ignore this node.

        Earth first! (We'll rob the other planets later)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://405352]
Approved by tachyon
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found