in reply to Re: whats wrong with my sub-routine ??
in thread whats wrong with my sub-routine ??

Isn't that the same as saying:

my $ar = qw/ foo bar baz /; func(\@ar); sub func { my $ar = @_; }

I ask, 'cause that's what he did.

--Coplan

Replies are listed 'Best First'.
Re: Re: Re: whats wrong with my sub-routine ??
by Ovid (Cardinal) on Apr 23, 2003 at 16:48 UTC

    Some misunderstandings:

    my $ar = qw/ foo bar baz /;

    In the line above, you're assigning a list in scalar context. You're going to get the last element of that list (baz) assigned to $ar.

    func(\@ar);

    That will throw a warning under strict because you declared a scalar ($ar), not an array (@ar).

    my $ar = @_;

    Now you're accessing an array in scalar context. This will result in $ar being set the the number of elements in @_. Here's a quick demonstration:

    $ perl -e 'sub aa {my $a = @_;print $a}; aa(qw(foo bar baz))' 3

    I realie those seem tricky and frankly, it's often a matter of memorizing how those things behave.

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

      Uhhh ... doesn't my $ar = @_; put the number of things, not the first thing, into $ar?

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        Uhhh ... isn't that what I said? :)

        my $ar = @_;

        Now you're accessing an array in scalar context. This will result in $ar being set the the number of elements in @_.

        Cheers,
        Ovid

        New address of my CGI Course.
        Silence is Evil (feel free to copy and distribute widely - note copyright text)