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

shift vs. @_ (where @_ evaluates to 1)

by P0w3rK!d (Pilgrim)
on May 09, 2003 at 17:56 UTC ( #256950=perlquestion: print w/replies, xml ) Need Help??

P0w3rK!d has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks,

Given this piece of code:

# a=463.3742.23 # b=99 sub getFoo{ my $a; my $b; my $strFoo; ($a,$b) = @_; $strFoo .= $a; $strFoo .= "_"; $strFoo .= $b; $strFoo .= "_testme"; $strFoo .= ".xml"; return $strFoo; } Result: $strFoo=463.3742.23_99_testme.xml works. However, given this piece of code:
# single value passed in = 463.3742.23 sub getFoo{ my $strFoo = @_; $strFoo .= "_testme"; $strFoo .= ".xml"; return $strFoo; } Result: $strFoo=463.3742.23_testme.xml *or* $strFoo=1_testme.xml
Why is this intermittent behavior occurring?


  • A. I am seeing weird behavior
  • B. their is a lack of caffine in my bloodstream
  • C. TGIF
  • D. I want to go see X2 && Halley's Berrys ;) lol
  • E. All of the above

Note: Running ActiveState Perl 5.8 build 508


Replies are listed 'Best First'.
Re: shift vs. @_ (where @_ evaluates to 1)
by pfaut (Priest) on May 09, 2003 at 17:58 UTC

    my $strFoo = @_ evaluates @_ in scalar context. Arrays in scalar context return their size. What you want is my ($strFoo) = @_ which evaluates @_ in list context returning its values.

    90% of every Perl application is already written.
      DoH! I had it that way and changed it.

      Thank you :)


Re: shift vs. @_ (where @_ evaluates to 1)
by dws (Chancellor) on May 09, 2003 at 18:12 UTC
    pfaut gave you a good answer, though there are still a couple of thing about your post worth commenting one.

    First, you showed code (good), but you didn't actually show how the code was being invoked. In this particular case, the actual invocation wasn't relevant, though it often is. Posting a small, complete fragment increases the odds of getting problems solved.

    Last, you can save space (and reader comprehension), by writing idiomatic Perl, like this:

    sub getFoo { my($a, $b) = @_; my $strFoo = $a; ...
    instead of
    sub getFoo { my $a; my $b; my $strFoo; ($a,$b) = @_; $strFoo .= $a;
    The former form maps arguments to lexical variables right away, which is friendly to your readers. Making people read further into your subroutine to find arguments increases the likelihood that people will misunderstand what you're doing.

      I understand your perspective, however most of the questions I ask are recoded to address the entire audience, not just the pros.

      Also, no one has ever lost track of my code, if that ever occurs, I will have to consider a leave-of-absence from the monestary, be caned, or disrobed by Hef's women, or something.

      -P0w3rK!d :)

Re: shift vs. @_ (where @_ evaluates to 1)
by Zaxo (Archbishop) on May 10, 2003 at 01:42 UTC

    You finction can be written more simply as

    sub getFoo{ my ($x,$y) = @_; $x . '_' . $y . '_testme.xml'; }
    or just:
    sub getFoo{ sprintf '%s_%s_testme.xml', @_; }
    Notice that the package global names $a and $b are not used. It's best to preserve them for sort.

    After Compline,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://256950]
Approved by Enlil
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2019-12-14 18:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found