Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Syntactic Confectionery Delight
 
PerlMonks  

Re: Function Prototype Context Conversion

by ariels (Curate)
on Jun 12, 2002 at 20:05 UTC ( #173988=note: print w/ replies, xml ) Need Help??


in reply to Function Prototype Context Conversion

...why can't @foo be treated as an implicit list for DWIM purposes?
Because you asked Perl to treat it specially!

Think of it like this: when you say sub foo($), you're telling Perl to convert to a scalar the first arg of foo. When you say ``foo(@foo)'', you know that @foo has only one element (and therefore can assume you know what you mean), but perl doesn't. All perl looks at is the syntax of your code: an array is being used in a scalar context. So it gets converted to a scalar. Your "rule" for converting only works for 1-element lists; Perl's rule for converting an array to a scalar works for any size array.

It looks like another behaviour of Perl's conversion semantics that manages to confuse people.


Comment on Re: Function Prototype Context Conversion
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2014-04-23 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (541 votes), past polls