There is hardly any difference between
my @foo; my %bar;
and
my @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.
#!/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
my $yuk;
and
my $yuk = {};
The former assigns an undefined value to $yuk, while the latter assigns a reference to an anonymous hash to $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


In reply to Re: Predeclaration by Abigail-II
in thread Predeclaration by hakkr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.