peculiar behaviour with prototyped routines
am 13.11.2007 09:49:41 von christian.hanselHi all,
Experimenting with prototyping I came across a rather peculiar and
inexplicable behaviour of routines depending on whether they are
defined in
the main file or a required library.
The following file :
#testlib.pl
#! /usr/bin/perl
sub test1(\@$) {
my @a=@{(shift)};
my $v=shift;
print "\n----------\nTest1:\n array: @a\n v:$v\n";
}
sub test2(\@$) {
my @a=@_;
my $v=pop @a;
print "\n----------\nTest2:\n array: @a\n v:$v\n";
}
print "\nin libfile..\n";
my @t1=(0,1,2,3,4,5,6,7,8,9);
test1(@t1,100);
test2(@t1,100);
1;
prints when called directly
>bash3.1 cvh@shogun::~$ perl testlib.pl
in libfile..
----------
Test1:
array: 0 1 2 3 4 5 6 7 8 9
v:100
----------
Test2:
array: ARRAY(0x804cf9c)
v:100
>bash3.1 cvh@shogun::~$
However when this file is included/required in another file the
functions
behave totally different, depending on whether the routine is called
from
where it is defined or from a higher hierarchy.
I doubt that this is intended:
The file testfile.pl
#! /usr/bin/perl
require "./testlib.pl";
print "\nin mainfile..\n";
my @t1=(0,1,2,3,4,5,6,7,8,9);
test1(@t1,100);
test2(@t1,100);
shows a reverse behaviour than that from before
bash3.1 cvh@shogun::~$ perl testfile.pl
in libfile..
----------
Test1:
array: 0 1 2 3 4 5 6 7 8 9
v:100
----------
Test2:
array: ARRAY(0x8060970)
v:100
in mainfile..
----------
Test1:
array:
v:1
----------
Test2:
array: 0 1 2 3 4 5 6 7 8 9
v:100
bash3.1 cvh@shogun::~$
As can be seen while the test1-routine does not correctly take the
provided
array parameter and instead hands @t1's first element to the second
parameter this time the second routine test2 behaves the same way
test1 did
when called from inside testlib.pl.
Can someone explain this rather peculiar behaviour or point me towards
my
mistake?. Or is this simply a bug in perl's prototyping?
Any hint or answer is greatly appreciated
CVH