Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Perl Idioms Explained - ${\$obj->method} and @{[sort @list]}

by Abigail-II (Bishop)
on Aug 21, 2003 at 15:58 UTC ( #285505=note: print w/ replies, xml ) Need Help??


in reply to Perl Idioms Explained - ${\$obj->method} and @{[sort @list]}

One must be careful with the ${\foo} because if foo returns something that isn't a simple scalar value then you might get unexpected results.

To be more precise, the \ operator gives its argument list context, which may surprise many people (it did me). The other interpolation construct @{[  ]} also provides list context. This is something to be aware of, and use of scalar will be appropriate in some cases, including in ${\scalar foo}.

Abigail


Comment on Re: Perl Idioms Explained - ${\$obj->method} and @{[sort @list]}
Select or Download Code
Re: Re: Perl Idioms Explained - ${\$obj->method} and @{[sort @list]}
by bradb (Sexton) on Aug 23, 2003 at 19:24 UTC
    To be more precise, the \ operator gives its argument list context, which may surprise many people (it did me).

    Why would this surprise you? :)

    If I ask for a reference to an array that contains ('foo', 'bar', 'baz'), I expect a reference to an array that has three elements in it, not the number 3 (as would be the case if \ provided scalar context for its arguments.)

      That doesn't explain it. \('foo', 'bar', 'baz') is the same as (\'foo', \'bar', \'baz'). Taking a reference to an array certainly doesn't behave that way.

      Makeshifts last the longest.

      Read Arrays are not lists and Scalars, Lists, and Arrays and repeat to yourself three times - "('foo', 'bar', 'baz') is not an array" ;-)

      I'm with Abigail-II. The \ operator has lots of weird cases that surprise you. For example:

      \@foo # reference to array @foo \(@foo, $a) # list of reference to array @foo and scalar $a \(@foo) # list of references to contents of @foo
        Read Arrays are not lists and Scalars, Lists, and Arrays and repeat to yourself three times - "('foo', 'bar', 'baz') is not an array" ;-)

        I can't be blamed if you don't read my post. ;-) I didn't say ('foo', 'bar', 'baz') was an array, I said If I ask for a reference to an array that contains ('foo', 'bar', 'baz').

        HTH.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-09-02 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (20 votes), past polls