Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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 chanting in the Monastery: (5)
As of 2015-08-01 02:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls