Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: map and return

by merlyn (Sage)
on Sep 03, 2009 at 10:14 UTC ( #793145=note: print w/replies, xml ) Need Help??

in reply to map and return

The behavior is consistent, even though the syntax is misleading. In both cases, precisely one level of subroutine call is being popped.

A similar situation exists when you compare a do { ... } while (...) loop with a while (...) { ... } loop: last/next/redo ignore the former (and act on an outer block), while they respect the latter as the innermost enclosing loop block.

Confusing to a beginner, but makes sense once you play with it for a bit.

-- Randal L. Schwartz, Perl hacker

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Replies are listed 'Best First'.
Re^2: map and return
by ELISHEVA (Prior) on Sep 03, 2009 at 10:40 UTC

    If map is meant by design to function as a flow of control token like while and foreach, then I would expect it to be documented as such in perlsyn, which it is not.

    Best, beth

      What is the prototype for map? It is undef because its arguments cannot be expressed by a prototype because the builtin does not really behave like a Perl function.

        As near as I can tell, no built-in function listed in index-functions has a prototype (just checked) and some of these behave more normally with regards to return. For instance, both map and sort are on that list. Neither map nor sort have defined prototypes, but sort treats the return inside the block as if it were returning from a subroutine, just like my custom sub foo above.

        # no compile problems here! my @x = sort { return $a <=> $b; } (3, 2, 1); print "sorted: (@x)\n"; #prints: sorted (1, 2, 3)

        Best, beth

        Update: Struck out incorrect portion of post - forgot to include "CORE::" in the function name, as pointed out by Anonymous Monk below.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://793145]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2018-07-23 07:54 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (459 votes). Check out past polls.