in reply to Using $# defines a variable?

Short answer: autovivification.

When you write $#{$array}, you're asking Perl what the last index of the array is which the reference $array points to, in other words, you're using the previously undefined $array like an array reference, so Perl autovivifies an anonymous array for you. See also item 3 under Using References.

use warnings; use strict; use Data::Dumper; my $array; print Dumper($array); print Dumper($#{$array}); print Dumper($array); __END__ $VAR1 = undef; $VAR1 = -1; $VAR1 = [];

Replies are listed 'Best First'.
Re^2: Using $# defines a variable?
by LanX (Saint) on May 18, 2019 at 22:14 UTC
    Additionally it's worth noting that $#name is an lvalue allowing to resize the array @name

    DB<2> @a=1..10 DB<3> $#a = 3 DB<4> x @a 0 1 1 2 2 3 3 4

    In other words autovivification is necessary to allow this feature.

    Lvalues can be passed around, Perl can't be sure if it's only used for a read access.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice