Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Unhappy returns

by Perl Mouse (Chaplain)
on Oct 10, 2005 at 12:53 UTC ( [id://498790]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Unhappy returns
in thread Unhappy returns

Well, map and grep are usually seen as functions, but there's no denying that both map and grep are looping constructs.

But ultimely, what is the difference between an operator, a function or a syntax construct from a programmers aspect of view (other than using it to answer "where do I find this in the manual")? Would you code differently if map or grep were reclassified as a syntax or looping construct, or foreach as a function?

Perl --((8:>*

Replies are listed 'Best First'.
Re^4: Unhappy returns
by Aristotle (Chancellor) on Oct 10, 2005 at 14:33 UTC

    The difference is that foreach is a statement, and as such does not return anything. The context in which its body is evaluated is therefore never of any interest, any more than it is of interest which context the body of an if or while is evaluated in.

    map and grep OTOH are expressions and do return something; which means context is paramount.

    Makeshifts last the longest.

      I'd have to disagree. Every runtime statement in perl does have a return value. It's not true that foreach doesn't return anything. Ponder this piece of code:
      @x = sub { 0; for (4,3,2) { 1 } }->();
      What does the sub return? Is it a bug? What should it return? (It has to return something, even if it's an empty list.) Also compare to
      @x = sub { }->();
      and
      $x = sub { 0; for (4,3,2) { 1 } }->();

        What does the sub return?

        The foreach in your examples is a red herring. A function call is an expression. And function calls return the value of the last expression evaluated. foreach is not an expression, but in the course of its execution several of those may be evaluated.

        My statement stands. (Pun intended.)

        Makeshifts last the longest.

        It's not true that foreach doesn't return anything.

        Sure it is. Otherwise, you would not have had to wrap your example for loops in a sub to try to make your point. If it returned something, you'd be able to write code like: @x = for (1) { 1 }; But, that's a syntax error. Why? Because statements don't return anything. Q.E.D.

        -sauoq
        "My two cents aren't worth a dime.";
        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2025-06-13 10:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.