[comp.lang.perl] Is it legal to assign an array to @_ inside a subroutine?

marcl@ESD.3Com.COM (Marc Lavine) (05/25/91)

When I run the enclosed script (with Perl 4.003), I get the following
output:

@a=9,8,7
@_=9,8,7
@a=1,2,3
@_=

instead of what I expected:

@a=9,8,7
@_=9,8,7
@a=1,2,3
@_=1,2,3

The assignment of a list to @_ seems to result in @_ becoming empty.
Is there something special about assigning to @_ inside a subroutine
or have I come across a bug?  Any insights would be appreciated.


#!/usr/local/bin/perl

sub f
    {
    @a = @_;
    print ( "\@a=", join ( ',', @a ), "\n" );
    print ( "\@_=", join ( ',', @_ ), "\n" );
    @a = ( 1, 2, 3 );
    @_ = ( 1, 2, 3 );
    print ( "\@a=", join ( ',', @a ), "\n" );
    print ( "\@_=", join ( ',', @_ ), "\n" );
    }

&f ( 9, 8, 7 );
--
Marc Lavine		    	Broken: marcl%3Com.Com@sun.com
Smart: marcl@3Com.Com	    	UUCP: ...{sun|decwrl}!3Com.Com!marcl

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (05/25/91)

In article <marcl.675151107@cook.ESD.3Com.COM> marcl@ESD.3Com.COM (Marc Lavine) writes:
: When I run the enclosed script (with Perl 4.003), I get the following
: output:
: 
: @a=9,8,7
: @_=9,8,7
: @a=1,2,3
: @_=
: 
: instead of what I expected:
: 
: @a=9,8,7
: @_=9,8,7
: @a=1,2,3
: @_=1,2,3
: 
: The assignment of a list to @_ seems to result in @_ becoming empty.
: Is there something special about assigning to @_ inside a subroutine
: or have I come across a bug?  Any insights would be appreciated.

As the doc said, don't do that.

@_ is a little too special currently to be treated like any other array.
In particular, if you assign to it, it's not smart enough to figure out
that you aren't talking about function argument references any more, and
it won't think it owns the pointers like a normal array does.

Don't do that.

Larry