in reply to a HASH ref while "strict refs" ERROR

In the second, showArray(@arr) appears before the prototype for showArray is known, so the prototype is ignored. In the first, showArray(@arr) becomes showArray(\@arr), but there's no change in the second snippet. Yet another reason to avoid prototypes. The following is a solution that's no less efficient and doesn't exhibit the problem regardless of the order in which the functions are defined:

#!/usr/bin/perl -w use strict; sub higherLevel { my(@arr) = @{(shift)}; showArray(\@arr); } sub showArray { my(@arr) = @{(shift)}; my($temp); foreach $temp (@arr) { print "$temp\n"; } } my(@arr) = ("one","two","three"); higherLevel(\@arr);

Replies are listed 'Best First'.
Re^2: a HASH ref while "strict refs" ERROR
by Roy Johnson (Monsignor) on May 04, 2005 at 17:57 UTC
    Considering that the first thing each sub does is copy all the elements of the array, there's not much point in passing a ref and dereferencing it. Just sling the arrays around.
    sub higherLevel { my(@arr) = @_; showArray(@arr); } sub showArray { my(@arr) = @_; foreach my $temp (@arr) { print "$temp\n"; } } my(@arr) = ("one","two","three"); higherLevel(@arr);

    Caution: Contents may have been coded under pressure.

      You're right

      sub test_arr { my @arr = @_; $a = join('|', @arr); } sub test_ref { my @arr = @{$_[0]}; $a = join('|', @arr); } sub test_ref2 { $a = join('|', @_); }
      TINY ARRAY Rate ref arr ref2 ref 120870/s -- -14% -17% arr 140004/s 16% -- -4% ref2 145146/s 20% 4% -- SMALL ARRAY Rate ref arr ref2 ref 61587/s -- -8% -58% arr 67214/s 9% -- -54% ref2 146895/s 139% 119% -- GIANT ARRAY Rate ref arr ref2 ref 850/s -- -0% -99% arr 850/s 0% -- -99% ref2 148539/s 17383% 17381% --

      The arrays would have to be pretty damn big for it to matter if one does my @arr = @_.