in reply to Length of Array Passed as Reference
The key issue is your parameter assignment in test_function. @_ is an array list so assignment in a scalar context will get you the length of the array list - in this case 1 because there is a single parameter passed to test_function. The fix is to use a list assignment:
use warnings; use strict; use feature qw/say /; my @test_array = ([1, 2, 3], [4, 5, 6]); test_function(\@test_array); sub test_function { my ($ref_array) = @_; say scalar @$ref_array; }
Prints:
2
Note too that in Perl since last century you don't need to call subs using &. In fact doing so can cause some very nasty to find bugs.
Dumping $ref_array instead of @_ may have helped you pick up the error.
Update: corrected brain fart - thanks haukex
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Length of Array Passed as Reference
by haukex (Archbishop) on Dec 10, 2020 at 09:16 UTC | |
|
Re^2: Length of Array Passed as Reference
by Leudwinus (Scribe) on Dec 10, 2020 at 02:26 UTC | |
by GrandFather (Saint) on Dec 10, 2020 at 02:36 UTC |