Perl Conditional Doubts

I have never studied Perl at any great depth, but I am doing the course of my work (can I say "just for fun"?). I understand the basics without any major problem, but now I will learn about links and shortening. I have doubts about shrinking arrays and accessing a reference value, consider the two scenarios below:

  •  

    @Codes =
            (
             ["A1W",
              ["A2Q","A2Z"]],
             ["B2R","BB3"]
            );
    $CodeRef = \@Codes;
    
    #full notation
    print @{@{@{$CodeRef}[0]}[1]}[1], "\n";
    
    #shorthand notation
    print $CodeRef->[0]->[1]->[1], "\n";
    
  •  

    my $BookPageArray;
    {
            $AnotherArray = [24, 18, 36]; 
            $Reference = \$AnotherArray; 
            $BookPageArray = \$AnotherArray; 
            $AnotherArray = [53, 256, 42];  count drops to 0
    }
    undef $Reference;
    print ${$BookPageArray}->[0]."\n";
    

My question is why, in the second example, the link $BookPageArrayin the last print statement contains curly braces around it, while in the first example the link $ CodeRef is not specified?

If I pull out the braces and the previous character $in the second example, this tells me that there is no array in this link ...

Is this because the second example uses anonymous data?

+4
4

:

$$BookPageArray->[0]."\n";

$, BookPageArray . , "$" , .

+5

, ! , .

, $BookPageArray . ?

. .

my $answer = 3 + 4 x 5 + 6;          # Example 1
my $answer = 3 + ( 4 x 5 ) + 6;      # Example 2
my $answer = ( 3 + 4 ) X ( 5 + 6 );  # Example 3

1 2 . ? , . 3 .

my $answer = 1 + 3 + 4 + 9 + 10;
my $answer = ( ( ( ( 1 + 3 ) + 4 ) + 9 ) + 10 );

, . , , ( ), ( ).

:

print $foo . "\n";
print ${foo} . "\n";

:

print "The file name is ${foo}_bar.txt\n";

, Perl , $foo, $foo_bar. :

print "The file name is ${foo_bar}.txt\n";
print "The file name is $foo_bar.txt\n";

, :

${$BookPageArray}->[0]."\n";

, , ${BookPageArray} , :

${$BookPageArray}->[0]."\n";   # I see that this is a scalar reference being dereferenced
$$BookPageArray->[0]."\n";     # Didn't notice the `$$`, so missed the dereferencing.
${${$BookPageArray}}[0]."\n";  # Yes, this is the same thing!

-> , .

my $value = $foo->{BAR};    # I can see that `$foo` is a reference to an array!
my $value = ${${foo}}{BAR}; # Ow! This hurts my brain
my $value = $$foo{BAR};     # Easier on the brain, but I'll miss that `$foo` is a ref.

, . , -> . , .

:

my $person = {};  # This will be a hash reference (not necessary)...
$person->{NAME} = "Bob";
$person->{ADDR} = "123 Testing Ave.";
$person->{CITY} = "New York";
$person->{STATE} = "CA"; #Got you!
$person->{PHONE} = [];  # Array Reference (not necessary)
$person->{PHONE}->[0] = {}; # Hash reference (not necessary)
$person->{PHONE}->[0]->{TYPE} = "cell";
$person->{PHONE}->[0]->{NUMBER} = "555-1234";

, , .

- -, , . my $person = {}; . my $person, , , . , , , , . $person->{PHONE}->[0] - , .

Perl. . Perl, Perl- Perl 3.x, Perl- - .

, , Object Oriented Perl tutorial. - Perl , , .

- : $person->{PHONE}->[0]->{NUMBER} = "555-1234";

+10

,

print @{@{@{$CodeRef}[0]}[1]}[1], "\n";  # Array slices

print ${${${$CodeRef}[0]}[1]}[1], "\n";  # Array lookups by index

.

print $CodeRef->[0]->[1]->[1], "\n";

print $CodeRef->[0][1][1], "\n";

.

+5

My question is why, in the second example, the $ BookPageArray link in the last print statement contains curly braces around it, while in the first example the $ CodeRef link is not specified?

In the second example, it $BookPageArrayhas a scalar link, but it $CodeRefhas an array reference. Everyone must dereference correctly ( @$$BookPageArrayvs @$CodeRef), otherwise you will get exceptions.

To clearly see the difference between scalar and array, check

perl -MData::Dumper -e '
  @arr = 10..14; 
  $aref = \@arr; 
  $sref = \$aref; 
  print Dumper $aref; 
  print Dumper $sref;
'

Exit

$VAR1 = [
          10,
          11,
          12,
          13,
          14
        ];
$VAR1 = \[
            10,
            11,
            12,
            13,
            14
          ];
+1
source

All Articles