Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: map and return

by merlyn (Sage)
on Sep 03, 2009 at 10:14 UTC ( [id://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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://793145]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-29 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found