Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Re: Re: Re: What should be returned in scalar context?

by BrowserUk (Patriarch)
on Dec 03, 2003 at 21:57 UTC ( [id://312029]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: What should be returned in scalar context?
in thread What should be returned in scalar context?

I'm puzzled by this

sub foo { # vvvv Temp array my @ret = map { # body of foo here $whatever; } @_; # <<< list 1 # vvvvvvvv list 2 @ret[0..$#ret]; # ^^^^^^^^^^^^^^ list 3 }

Why generate one temporary array and three lists to achieve the same effect as

sub foo { map { # body of foo here $_[ $_ ] ...; $whatever; } 0..$#ret; }

which creates 2 lists and no temporary array.

What am I missing?


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!

Replies are listed 'Best First'.
Re^5: What should be returned in scalar context? (missing?)
by tye (Sage) on Dec 04, 2003 at 06:36 UTC
    What am I missing?

    Note that the node title mentions "scalar context". This is what you are missing. Compare your two foo()s when called in a scalar context.

                    - tye

      Right tye, ysth pointed this out to me by /msg also.

      If the original version is called in a scalar context when returning the list 'foo', 'bar', 'baz', the result is 'baz'.

      My version returns 3.

      Now the question is why?

      Why preserve or emulate the list-in-a-scalar-context semantics?

      Is there ever a situation when arbitrarially returning the last physical value in a list is the right thing to do?

      And if anyone knows of such an occasion, does this occur so frequently that it is worth while imposing the additional costs on every other use of the sub in order to avoid the need to do

      my $last = ( foo() )[ -1 ];

      which would be hugely clearer as to the desired result than my $last = foo(); as well as considerably more efficient for the general case.

      I've always considered this side-effect of a list in a scalar context to be terminally broken and something that really ought to be fixed. The argument against fixing it is probably backwards compatibility, but I would argue that given the rarity that this behaviour is likely to be used and depended upon by real code, and that it is so easy to achieve the equivalent with a simple modification that increases the clarity of the desired result so much, that it would be better for everyone to force the fix upon existing code that relies upon this behaviour.

      I find the idea of coding a sub in such a convoluted manner in order to perpetuate this broken behaviour an anathema.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      Hooray!
      Wanted!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found