You are on page 1of 3

Perl Notes

Variable:
1. Scalar variable: These variables contain single value that can be a number or string. a. Number variable o $var=5; o An expression can also be assigned to a scalar variable. Example : $var=3+4; b. String variable o $var=Santosh o If a string has a space, then it must be double quoted. $var= Santosh Prasad 2. Array Variable: An array is a collection of data of various types. Example : @array=(1,santosh,2.5); Note: Above array has got integer, string and floating number. An array can be single dimensional or multi-dimensional. Single dimensional array declaration : @array= (list of values separated by comma); It can also be assigned individually @array_name[index]=value; @array*0+=santosh; Array index in Perl always starts from 0. o Using array to assign values. Single value $val=@array_name[index] Multiple values ($var1,$var2)=(@ array_name); 3. Manipulating arrays: we can manipulate arrays using following built-in function. o Push: this function add a value at the end of the array. Syntax : push (@array, list of values); o Pop: this function removes a value at the end of an array. Syntax : pop(@array); o Shift : removes first element of an array Syntax : shift(@array); o

o o o o

Unshift: add element at the beginning of the array. Syntax :unshift (@array, list of values) Reverse : reverse the array Syntax: reverse(@array); Sort: sort the elements within array Syntax :sort(@array) Splice: this function help in deleting and replacing the specified element within an array and returned the deleted or replaced element. Syntax : splice(@array ,x ,y) Where x : is the index number & y is no of element to be deleted/replaced.

True and False in Perl


1. If it is evaluated in numeric context then 0 (Numeric zero) will be evaluated as false. 2. In string context the zero (0) will be evaluated as false. Note: Double quote around the numeric zero is mandatory in the string context. 3. Null value /empty variable are evaluated as false. 4. Empty list (); are evaluated as false. 5. String undef is treated as false and rest all are true. 6. We can use ! mark to turn the truth into false.

Some (of many) ways to get at reference data There are several different ways to 'dereference' Given a reference called $array_ref (to our favorite array);

my @new_array = @{$array_ref} # get entire array and assign to new variable my $list_element = ${$array_ref}[1] # get '1th' element from the array referenced by $array_ref my $list_element = $array_ref->[1] # same thing with 'arrow syntax'

Given a reference called $hash_ref (to our favorite hash);


my %hash_copy = %{$hash_ref}; # get a new copy of the hash referenced by $hash_ref my $hash_value = ${$hash_ref}{'some_key'}; # assign the value associated with 'some_key' in the hash referenced by $hash_ref my $hash_value = $hash_ref->{'some_key'}; # same thing!!

Given a reference called $my_cool_subroutine (to our favorite subroutine)


my $result = &{$my_cool_subroutine}($arg1,$arg2); #invoke &my_cool_subroutine with two arguments my $result = $my_cool_subroutine->($arg1,$arg2); # look familiar??

Instantiating Variable the scalar $scalar @list

Instantiating a reference to it (Anonymous)

Referencing Dereferencing Accessing an it it element $ref = \$scalar $ref = \@list $$ref or ${$ref} @{$ref} N/A ${$ref}[3] $ref->[3] ${$ref}{"president"} $ref->{"president"}

$scalar = "steve"; $ref = \"steve"; @list = ("steve", "fred"); $ref = ["steve", "fred"];

%hash

%hash = ("name" $hash = {"name" => "steve", => "steve", $ref = "job" => "job" => \%hash "Troubleshooter"); "Troubleshooter"}; $ref = \*FILE

%{$ref} {$ref} or scalar <$ref>

FILE

You might also like