Perl Variables

$var    a simple scalar variable.
$var[28]    29th element of array @var.
$p=\@var    now $p is a reference to array @var.
$$p[28]    29th element of array referenced by $p. also, $p->[28].
$var[-1]    last element of array @var.
$var[$i][$j]    $jth element of the $ith element of array @var.
$var{‘Feb’}   one value from hash (associative array) %var.
$p=\%var    now $p is a reference to hash %var.
$$p{‘Feb’}    a value from hash referenced by $p. also, $p->{‘Feb’}.
$#var    last index of array @var.

@var    the entire array; in a scalar context, the number of elements in the array.
@var[3,4,5]    a slice of array @var.
@var{‘a’,’b’}    a slice of %var; same as ($var{‘a’},$var{‘b’}).
%var   the entire hash; in a scalar context, true if the hash has elements.

$var{‘a’,1,…}    emulates a multidimensional array.
(‘a’…’z’)[4,7,9]   a slice of an array literal.

PKG::VAR a variable from a package, e.g., $pkg::var, @pkg::ary.

OBJECT reference to an object, e.g., \$var, \%hash.

*NAME refers to all objects represented by NAME.
*n1=*n2 makes n1 an alias for n2.
*n1=$n2 makes $n1 an alias for $n2.

 

Example:
@A = ( $x =~ /<(.*),(.*)>/ );
@array=”I\nam\nAli\nBaba”;
@days = (“Mo”, “Di”, “Mi”, “Do”, “Fr”, “Sa”, “So”);
$x = @days ergibt $x = 7;
$x = (“Mo”, “Di”, “Mi”; ergibt aber $x=”Mi”
$#days ist der letzte Index. (hier 6 )

  • ($first,$second,@rest) = @days;
  • ($first,$second,$third) = @days;
  • ($first) = @days;  $last = pop(@days);
  • $first = shift(@days);
  • shift(@days);
  • unshift(@days,”neu”);ergibt(“neu”,”Mo”,”Di”,”Mi”,”Do”,”Fr”,”Sa”,”So”);

Dereferencing references

$sref string that contains scalar reference
$$sref value of what reference refers to
$lref string that contains a list reference
@$lref list that $lref refers to
$$lref[$i] dereference of a particular list element
$lref->[$i] Same as above
$href string that contains a hash reference
%$href dereference of $href
$$href{key} dereference of a hash element
$href->{key} Same as about

Special Arrays

@ARGV Contains the command-line arguments for the script (not including the command name).
@EXPORT Names the methods a package exports by default.
@EXPORT_OK Names the methods a package can export upon explicit request.
@INC Contains the list of places to look for Perl scripts to be evaluated by the do FILENAME and require commands.
@ISA List of base classes of a package.
@_  Parameter array for subroutines. Also used by split if not in array context.
%ENV Contains the current environment.
%INC List of files that have been included with require or do.
%OVERLOAD Can be used to overload operators in a package.
%SIG Used to set signal handlers for various signals.

Leave a Reply

Your email address will not be published. Required fields are marked *

*