yangyu has asked for the wisdom of the Perl Monks concerning the following question:

hi monks,
this is the code, array_argument_testing.pl
#!/usr/bin/perl @a=(9,2,3,4); @b=("a","b"); func(\@a,\@b); sub func { ($c,$d) = @_; print $#{@$c}+1,$#{@$d}+1; }
result from run at windows:
C:\pl>perl array_argument_testing.pl 00
result from run at unix:
$ perl array_argument_testing.pl 42
The result from unix is what I want,but what's the problem that the result is different from windows?

thanks for your help
YangYu

Replies are listed 'Best First'.
Re: same code,run at windows and unix separately,the result is different
by ikegami (Patriarch) on Jun 10, 2009 at 05:21 UTC

    This error that would have been prevented by use strict; use warnings;

    I bet your Windows version is 5.10 and your unix version predates 5.10.

    >c:\progs\perl588\bin\perl -l 770193.pl 42 >c:\progs\perl589\bin\perl -l 770193.pl 42 >c:\progs\perl5100\bin\perl -l 770193.pl 00

    Before 5.10, a bug caused the "@" in "$#{@a}" to be ignored. It was fixed in 5.10.0.

    Fixed:

    #!/usr/bin/perl use strict; use warnings; my @a = (9, 2, 3, 4); my @b = ("a", "b"); func(\@a, \@b); sub func { my ($c, $d) = @_; print $#{$c}+1, $#{$d}+1, "\n"; #print $#$c+1, $#$d+1, "\n"; \ #print 0+@{$c}, 0+@{$d}, "\n"; > Alternatives #print 0+@$c, 0+@$d, "\n"; / }

    See Dereferencing Syntax.

    Update: Added alternatives and link.
    Update: Added results of various version.

Re: same code,run at windows and unix separately,the result is different
by syphilis (Archbishop) on Jun 10, 2009 at 05:25 UTC
    Prints '42' for me on windows, as expected.
    The code being run on Windows must be different to the code being run on linux - otherwise output would be the same.

    Cheers,
    Rob

      As expected? The array @4 has zero elements, not four. Similarly, the array @2 has zero elements, not two.

        As expected?

        Correction ... make that "as I expected" .... having not taken the time to look too closely or think too hard about it. (Why wasn't the same bugfix applied to perl 5.8.9 ?)

        Cheers,
        Rob