Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
# (this gives key-value pairs: 'it', 'ciao', 'fr', 'salut')
Esto es especialmente cierto de las referencias. Para utilizar un valor de referencia, puede
combinar los sigilos juntos.
my @array = 1..5;
# This is an array
my $reference_to_an_array = \@array; # A reference to an array is a singular value
push @array, 6;
# push expects an array
push @$reference_to_an_array, 7; # the @ sigil means what's on the right is an array
# and what's on the right is $reference_to_an_array
# hence: first a @, then a $
Aquí hay una manera quizás menos confusa de pensar en ello. Como vimos anteriormente,
puedes usar llaves para envolver lo que está a la derecha de un sigilo. Así que puedes pensar en
@{} como algo que toma una referencia de matriz y te da la matriz de referencia.
# pop does not like array references
pop $reference_to_an_array; # ERROR in Perl 5.20+
# but if we use @{}, then...
pop @{ $reference_to_an_array }; # this works!
Como resultado, @{} realidad acepta una expresión:
my $values = undef;
say pop @{ $values }; # ERROR: can't use undef as an array reference
say pop @{ $values // [5] } # undef // [5] gives [5], so this prints 5
... y el mismo truco funciona para otros sigilos, también.
# This is not an example of good Perl. It is merely a demonstration of this language feature
my $hashref = undef;
for my $key ( %{ $hashref // {} } ) {
"This doesn't crash";
}
... pero si el "argumento" a un sigilo es simple, puedes dejar las llaves.
say $$scalar_reference;
say pop @$array_reference;
for keys (%$hash_reference) { ... };
Las cosas pueden volverse excesivamente extravagantes. Esto funciona, pero por favor Perl
responsablemente.
my %hash = (it => 'ciao', en => 'hi', fr => 'salut');
my $reference = \%hash;
my $reference_to_a_reference = \$reference;
my $italian = $hash{it};
# Direct access
my @greets = @$reference{'it', 'en'};
# Dereference, then access as array
my %subhash = %$$reference_to_a_reference{'en', 'fr'} # Dereference ×2 then access as hash
https://riptutorial.com/es/home 125