15.03.2020 Views

perl-language-es

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

close $log or die $!;

Los typeglobs también se pueden usar para hacer variables globales de solo lectura, aunque el

use constant es un uso más amplio.

# Global constant creation

*TRUE = \('1');

our $TRUE;

say $TRUE; # 1

$TRUE = ''; # dies, "Modification of a read-only value attempted"

# use constant instead defines a parameterless function, therefore it's not global,

# can be used without sigils, can be imported, but does not interpolate easily.

use constant (FALSE => 0);

say FALSE; # 0

say &FALSE; # 0

say "${\FALSE}"; # 0 (ugh)

say *FALSE{CODE}; # CODE(0xMA1DBABE)

# Of course, neither is truly constant when you can manipulate the symbol table...

*TRUE = \('');

use constant (EVIL => 1);

*FALSE = *EVIL;

Sigilos

Perl tiene una serie de sigilos:

$scalar = 1; # individual value

@array = ( 1, 2, 3, 4, 5 ); # sequence of values

%hash = ('it', 'ciao', 'en', 'hello', 'fr', 'salut'); # unordered key-value pairs

&function('arguments'); # subroutine

*typeglob; # symbol table entry

Estos parecen sigilos, pero no son

\@array; # \ returns the reference of what's on the right (so, a reference to @array)

$#array; # this is the index of the last element of @array

Puedes usar llaves después del sigilo si así lo deseas. Ocasionalmente, esto mejora la legibilidad.

say ${value} = 5;

Mientras usa diferentes sigilos para definir variables de diferentes tipos, se puede acceder a la

misma variable de diferentes maneras en función de los sigilos que use.

%hash;

# we use % because we are looking at an entire hash

$hash{it}; # we want a single value, however, that's singular, so we use $

$array[0]; # likewise for an array. notice the change in brackets.

@array[0,3]; # we want multiple values of an array, so we instead use @

@hash{'it','en'}; # similarly for hashes (this gives the values: 'ciao', 'hello')

%hash{'it','fr'}; # we want an hash with just some of the keys, so we use %

https://riptutorial.com/es/home 124

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!