Recently I noticed something odd with map and return. According to perlsub, an initial & in a prototype allows you to define a subroutine that emulates the syntax of Perl built-ins like map and grep. And yet there seem to be subtle differences, like the behavior of return within the block. For instance, in the case of map the block after the subname is scoped as if it were part of the surrounding code rather than the body of an anonymous sub.

The following code, placed inside a subroutine will cause the subroutine to return immediately. If placed at the top level of the script it will cause the Perl compiler to complain "Can't return outside subroutine ..." (Perl version 5.8.8; warnings, strict turned on of course).

my @x = map { return $_; } (1,2,3); print "(@x)\n";

However, when the same block { return $_; } is called via a user defined sub that does essentially the same thing as map there are no compiler complaints nor precipitous returns:

sub foo(&@) { my ($crSub, @aParams) = @_; my @aResult; push(@aResult, $crSub->($_)) foreach @aParams; return @aResult; } #this outputs "(1 2 3)" my @x = foo { return $_; } (1, 2, 3); print "(@x)\n";

perlsub says that the user defined routines parse "almost exactly like". Almost isn't exactly. Why the difference in behavior? And what other subtle differences are there between built-in tokens like map and user defined subroutines with a (&@) prototype? The return behavior of map ate up a good deal of debugging time the other day because I just assumed that the block after map was just like an anonymous subroutine - which it clearly isn't. I'm hoping to save time in the future with a heads up on other differences hidden behind the phrase "almost exactly".

Many thanks in advance, beth


In reply to map and return by ELISHEVA

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.