develooper Front page | perl.perl5.porters | Postings from July 2003

Re: (func()){key}?

Thread Previous
From:
Michael G Schwern
Date:
July 30, 2003 20:08
Subject:
Re: (func()){key}?
Message ID:
20030731030756.GA22280@windhund.schwern.org
On Thu, Jul 31, 2003 at 03:37:43AM +0100, hv@crypt.org wrote:
> Michael G Schwern <schwern@pobox.com> wrote:
> :I'm sure this has come up before, but I'm curious.  Why doesn't this DWIM:
> :
> :    sub foo {
> :        return( this => 42, that => 23 );
> :    }
> :    
> :    $this = (foo()){42};
> :
> :Similar to:
> :
> :    sub foo {
> :        return(42, 23);
> :    }
> :
> :    $this = (foo())[1];
> 
> Well, that'd be because a) we don't have a WANTHASH context and b) you
> can't do C<< $a = ( this => 42, that => 23 ){42}; >>.

Let me to make myself more clear.  I'm looking for why we *can't* have A and B.
Assuming we all wanted it, is there any reason why it couldn't be added to the
language?  Is there a syntax conflict?  Or is it just a SMOP?


> Given that you can do this:
>   sub foo { return { this => 42, that => 23 } };
>   $this = foo()->{42};
> .. and more efficiently at that, I've never felt the lack to be a problem.

Obi van der Kenobi: *hand wave* This isn't the language feature you're 
                    looking for.
Schwern the Hutt:   Your tricks will not work on me, Jedi.


If there were no problem then we'd all be writing:

    sub foo { return [ 42, 23 ] }
    $this = foo()->[1];

but (foo)[1] is by far the more common idiom.  This may be because its
usually more convenient to return an array than an array ref.

Also, you assume one has control of the interface of foo().  If I have
something which simply returns a hash, I have to copy it into a hash ref...

    $this = {foo()}->{this};

and suddenly things are even less efficient.

Finally, simple consistency says that if (...)[$i] works why not (...){$k}?


-- 
It's Flypaper Licking time!

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About