http://qs321.pair.com?node_id=1218802


in reply to About personal function

(First of all, the code you gave throws the error message "Illegal declaration of anonymous subroutine", which would be fixed by removing the & from the sub declaration. Also, if "@test" is changed to "my @test" in line 8, the code will work under "use strict" and "use warnings", both of which may be helpful to you later on.)

When your &test routine is entered, @_ will contain the contents of both @x and @y, flattened into a single array (i.e., @_ contains ( "A", "B", "C", "C", "D", "F", ) ). You, however, are currently assigning to @temp the value of @_[0] (better written as $_[0]), which is the first entry of @_ (or "A"). There are two ways I can see going. The simplest is to change that line to something like @test = @_;, wherein you will get the string "A B C C D F".

If you want to get them as separate arrays, then you can pass them by reference, and then loop through the elements in @test in the loop (example derivation below; the -l switch appends an appropriate EOL for each print statement).

Original code (as 1-liner for testing):

$ perl -le 'my (@x, @y); @x = ( "A", "B", "C" ); @y = ( "C", "D", "F" +); &test( @x, @y, ); sub &test { @test = @_[0]; print "@test\n"; }' Illegal declaration of anonymous subroutine at -e line 1.

Modification to fix "Illegal declaration" error:

$ perl -le 'my (@x, @y); @x = ( "A", "B", "C" ); @y = ( "C", "D", "F" +); &test( @x, @y, ); sub test { @test = @_[0]; print "@test\n"; }' A

Add "strict" and "warnings" (via -M switches):

$ perl -Mstrict -Mwarnings -le 'my (@x, @y); @x = ( "A", "B", "C" ); @ +y = ( "C", "D", "F" ); &test( @x, @y, ); sub test { @test = @_[0]; pr +int "@test\n"; }' Variable "@test" is not imported at -e line 1. Possible unintended interpolation of @test in string at -e line 1. Variable "@test" is not imported at -e line 1. Global symbol "@test" requires explicit package name (did you forget t +o declare "my @test"?) at -e line 1. Global symbol "@test" requires explicit package name (did you forget t +o declare "my @test"?) at -e line 1. Execution of -e aborted due to compilation errors.

Fix errors from adding "strict" and "warnings":

$ perl -Mstrict -Mwarnings -le 'my (@x, @y); @x = ( "A", "B", "C" ); @ +y = ( "C", "D", "F" ); &test( @x, @y, ); sub test { my @test = @_[0]; + print "@test\n"; }' Scalar value @_[0] better written as $_[0] at -e line 1. A

Fix "Scalar value better written as" warning:

$ perl -Mstrict -Mwarnings -le 'my (@x, @y); @x = ( "A", "B", "C" ); @ +y = ( "C", "D", "F" ); &test( @x, @y, ); sub test { my @test = $_[0]; + print "@test\n"; }' A

Fix to display all of @x and @y, instead of the first element only:

$ perl -Mstrict -Mwarnings -le 'my (@x, @y); @x = ( "A", "B", "C" ); @ +y = ( "C", "D", "F" ); &test( @x, @y, ); sub test { my @test = @_; pr +int "@test\n"; }' A B C C D F

Changing to pass @x and @y in by reference (will stringify the reference-NOT what you wanted):

$ perl -Mstrict -Mwarnings -le 'my (@x, @y); @x = ( "A", "B", "C" ); @ +y = ( "C", "D", "F" ); &test( \@x, \@y, ); sub test { my @test = @_; +print "@test\n"; }' ARRAY(0x20071958) ARRAY(0x200718e0)

Turn stringified references into one line per array:

$ perl -Mstrict -Mwarnings -le 'my (@x, @y); @x = ( "A", "B", "C" ); @ +y = ( "C", "D", "F" ); &test( \@x, \@y, ); sub test { my @test = @_; +foreach my $arr ( @test ) { print "@{$arr}"; } }' A B C C D F

Hope that helps.