Empty hash declaration

I never knew the difference, if any, between the following:

my %hash;
my %hash = ();

Can anyone shed some light on this?

+4
source share
2 answers

In some languages, new variables are provided uninitialized. In Perl, scalars are created undefined, and arrays and hashes are created empty.

The second is wasteful. Assigning an empty list to an empty hash has no effect.

+5
source

You are absolutely right. There is no difference. A hash (or array) declaration creates an empty data structure.

- , undefined. , .

use Data::Dumper;

my $scalar;
my $scalar2 = '';

print Dumper \$scalar;
print Dumper \$scalar2;

my %hash;
my %hash2 = ();
print Dumper \%hash;
print Dumper \%hash2;

my @array;
my @array2 = ();

print Dumper \@array;
print Dumper \@array2;

defined , :

" ( ) . , . Perl. :"

+4

Source: https://habr.com/ru/post/1568146/


All Articles