in reply to Predeclaration
andmy @foo; my %bar;
Both will declare the variables at compile time, and assign an empty list to them at run time. Using Devel::Peek shows there is no difference.my @foo = (); my %bar = ();
#!/usr/bin/perl use strict; use warnings 'all'; use Devel::Peek; my @foo_1; my @foo_2 = (); my %bar_1; my %bar_2 = (); print STDERR "\@foo_1:\n"; Dump (\@foo_1); print STDERR "\@foo_2:\n"; Dump (\@foo_2); print STDERR "\%bar_1:\n"; Dump (\%bar_1); print STDERR "\%bar_2:\n"; Dump (\%bar_2); __END__
@foo_1:
SV = RV(0x81584b4) at 0x814490c
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x814ead4
SV = PVAV(0x8145e5c) at 0x814ead4
REFCNT = 2
FLAGS = (PADBUSY,PADMY)
IV = 0
NV = 0
ARRAY = 0x0
FILL = -1
MAX = -1
ARYLEN = 0x0
FLAGS = (REAL)
@foo_2:
SV = RV(0x81584b4) at 0x814490c
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x814eaec
SV = PVAV(0x8145e28) at 0x814eaec
REFCNT = 2
FLAGS = (PADBUSY,PADMY)
IV = 0
NV = 0
ARRAY = 0x0
FILL = -1
MAX = -1
ARYLEN = 0x0
FLAGS = (REAL)
%bar_1:
SV = RV(0x81584b4) at 0x814490c
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x814eb10
SV = PVHV(0x81535b8) at 0x814eb10
REFCNT = 2
FLAGS = (PADBUSY,PADMY,SHAREKEYS)
IV = 0
NV = 0
ARRAY = 0x0
KEYS = 0
FILL = 0
MAX = 7
RITER = -1
EITER = 0x0
%bar_2:
SV = RV(0x81584b4) at 0x814490c
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x814eba0
SV = PVHV(0x81535f0) at 0x814eba0
REFCNT = 2
FLAGS = (PADBUSY,PADMY,SHAREKEYS)
IV = 0
NV = 0
ARRAY = 0x0
KEYS = 0
FILL = 0
MAX = 7
RITER = -1
EITER = 0x0
However, there is a big difference between
andmy $yuk;
The former assigns an undefined value to $yuk, while the latter assigns a reference to an anonymous hash to $yuk.my $yuk = {};
Devel::Peek will show the difference:
#!/usr/bin/perl use strict; use warnings 'all'; use Devel::Peek; my $yuk_1; my $yuk_2 = {}; print STDERR "\$yuk_1:\n"; Dump ($yuk_1); print STDERR "\$yuk_2:\n"; Dump ($yuk_2);
$yuk_1:
SV = NULL(0x0) at 0x814eacc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
$yuk_2:
SV = RV(0x8158458) at 0x814eae4
REFCNT = 1
FLAGS = (PADBUSY,PADMY,ROK)
RV = 0x814490c
SV = PVHV(0x8153550) at 0x814490c
REFCNT = 1
FLAGS = (SHAREKEYS)
IV = 0
NV = 0
ARRAY = 0x0
KEYS = 0
FILL = 0
MAX = 7
RITER = -1
EITER = 0x0
Abigail
|
|---|