Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Function Prototype Context Conversion

by chromatic (Archbishop)
on Jun 12, 2002 at 19:57 UTC ( #173982=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 that would make the following code terribly ambiguous:

my @foo = ( 1 .. 5 ); my $list-to-scalar = @foo; my $length-of-array = @foo;

Replies are listed 'Best First'.
Re^2: Function Prototype Context Conversion
by tadman (Prior) on Jun 12, 2002 at 20:05 UTC
    No, that much is clearly understood. What I mean is that with respect to function prototypes, passing an array to a scalar prototype would seem to be a type mismatch, although these are kind of relative in such a loosely typed language as Perl. You'd hope for a warning, perhaps. You've asked for a scalar, but you're getting a list.

    What I was expecting was that the @foo array would be expanded into the brackets in the function call and passed through as a list, but Perl appears to be doing some truly clever things to try and handle the prototype, taking it to the ultimate extreme.

    japhy and everyone else who has been kind enough to post remarks on this subject is 100% correct, of course. What led to this insight into prototypes was one part ass, one part you, one part me, and we all know what that adds up to.

    The counter-intuitive component of this is that I'd expected an automatic array-to-list conversion to be peformed before said list is then passed to the prototype, but this is not the case. A list of an array is not the same as a list of the array elements.
      tadman: You've asked for a scalar, but you're getting a list.

      Ah, not so! You've ask for a scalar, but you're getting an array! If you had done:

      sub foo ($) { ... } @a = (1,10,100); foo(@a); # no error, '3' is passed to foo() foo(1,10,100); # error, too many args
      you'd have seen. Arrays and lists are not the same thing.

      Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2021-07-30 17:11 GMT
Find Nodes?
    Voting Booth?

    No recent polls found