Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
The stupid question is the question not asked
 
PerlMonks  

Re^3: Unhappy returns

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

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


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 10: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
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.