in reply to Re: Enforcing formal arguments at compile time?
in thread Enforcing formal arguments at compile time?

Actually prototypes don't always work the way you might expect (see links in node below):

#!/usr/bin/perl use strict; sub foo($); my @ary = (4,3,2); foo(@ary); # no compile time error sub foo($) { print shift }

Update

gmax points out I should run my code before posting as I forgot the my to satisfy strict. Ooops :-)

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Re: Enforcing formal arguments at compile time?
by pdcawley (Hermit) on May 03, 2002 at 11:44 UTC
    I expect that in the example you give, foo will be called with a single scalar argument which is a reference to the array @ary. Am I wrong? Or isn't that what you expect?

    Remember arrays are not lists. This is one of those occasions when forgetting that fact can leave you vaguely confused. Of course, if you've been following perl 6 you'd want to write that as foo(*@ary).

    Update: Dang! I am wrong. I didn't expect it to be a compile error, but the direction Perl 6 is taking led me astray as to what would actually happen. Ah well.

      Am I wrong?

      Yes, you are wrong.

      foo() is called with a single scalar argument which is precisely equal to scalar @ary or in other words the number of elements in @ary rather than a ref to it. I suggest you might like to 1) run the sample code where we print shift in foo and 2) follow the links I presented to learn more. I have a fair handle on scalar and array context BTW

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Yick! I stand corrected. The arrayref version is sub foo (\@) {...} isn't it?

        I think I've been playing with Perl 6 a little too much recently...