Understanding Links in Perl

I'm trying to understand the tutorial on links in Perl

perldoc perlreftut

So far, with the code below, I initialize an empty hash with

my %table

Here is the whole program

#!/usr/bin/perl -w 
use strict;

my %table;

while (<DATA>) {
chomp;
my ($city, $country) = split /, /;
#$table{$country} = [] unless exists $table{$country};
push @{$table{$country}}, $city;

print @{$table{$country}};
}



__DATA__
Chicago, USA
Frankfurt, Germany
Berlin, Germany
Washington, USA
Helsinki, Finland
New York, USA

Can someone explain the line below to me because I am confused since I see the link (I think) here, but it was initialized as a hash with% table.

push @{$table{$country}}, $city;
+4
source share
2 answers

You declare a hash %table. A declaration is when you tell Perl that there is a lexically bounded variable. Initialization is when you assign a value to a variable for the first time. You did not initialize it, so Perl sets the default value. Since this is a hash, it starts with an empty list ()that is false.

.

push @{$table{$country}}, $city;

, $table{$country} , , $city . , auto-vivification, ref , .

, , :

%table = ( 'USA' => [ 'Chicago' ] )

%table , USA .

Perl, . . .

+8

%table - ,
$table{$country} - , @{ $table{$country} } - .

" ", " " "HoA".


? autovivified, , Perl , $table{$country} .

,

push @{ $table{$country} }, $city

push @{ $table{$country} //= [] }, $city

, push

%table = ( 'USA' => [ 'Chicago' ] );

Docs:

+7

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


All Articles