Tuesday, July 3, 2018

perl cook book


perl cookbook:

1. Removing leading & trailing white spaces from string:

$string = trim($string);
@many = trim (@many);

sub trim {
            my @out = @_;
           
            foreach (@out) {
                        s/^\s+//;
                        s/\s+$//;
            }
            return ((@out==1) ? $out[0] : @out);
}

sub trim {
            my @out = @_;
            my $cond;

            foreach (@out) {
                        s/^\s+//;
                        s/\s+$//;
            }
           
            $cond = scalar (@out);
            if ($cond == 1) {
                        return $out[0];
            } else if ($cond >1) {
                        return @out;
            } else {
                        print "ERROR: Nothing is parsed to the subroutine\n";
            }
}

================================================================
2. Checking PVT has the PffV1300Tm40, PttV1200T025, PssV1100T125

chomp $_;
warn " Please enter valid PVT corner" unless /^P(\w+)V(\d+)Tm?(\d+)$/;

================================================================
3. Finding today's date:

use Time::localtime;
$tm =localtime;

($DAY, $MONTH, $YEAR) = ($tm->mday, $tm->mon, $tm->year);

================================================================
4. Day in a Week/Month/Year OR Week Number:

page 99

================================================================
5. Sleep function: (sleep less than a second)

use Time::HiRes;

while (<>) {
            sleep(0.25);     
}
================================================================
6. Printing all Environment variables used by perl:

#!/usr/bin/perl -w

foreach  my $var (sort keys %ENV) {
            print "$var = $ENV{$var}\n";
}
================================================================
7. Default variable = $_ and Default array = @_;

while () {
            chomp;

            foreach (split) {              ## $_ is split on whitespace, stores into @_
                        $_ = reverse;
                        print;
            }
}
================================================================
8. Iterating over an Array by reference:

@fruits = ("apple" , "orange");

$ref = \@fruits;  # Taking the reference to an array

foreach  $fruit  (@$ref)  {   ## $ref is the reference;  @$ref is an array by reference
            print  " $fruit  \n";
}

for ($i =0; $i < $#@$ref; $i++) {
            print " $ref->[$i]  \n";
}
================================================================
9. Extracting Unique elements from a list:

a) Straightforward:
           
            %seen = ( );
            @uniq = ( );

            foreach (@uniq)  {
                 unless ($seen{$_})   {
                        $seen{$_} = 1;              ## If we get here, we have not seen it before
                        push (@uniq, $_);         ## push that element to array
                }
            }

b) Faster:

            %seen = ( );
           
            foreach $item (@list) {
                        push (@uniq, $item ) unless $seen{$item}++;  ## It creates a new entry in the hash everytime it sees an element that hasn't seen before
            }

c) Faster but different:
           
            %seen = ( );
           
            foreach $item (@list)  {
                        $seen{$item} ++;
            }
           
            @uniq = keys %seen;

d) Faster:

            %seen = ( );
            @uniq = grep (! $seen{$_}++ }  @list;   ## merges the construction of hash & extraction of unique elements ==> same as 1st approach
           
================================================================
10. Find the elements in @A that aren't in @B:
            i.e., unique elements in A compared to B array

Solution:
            1. Mark all B array elements by creating %seen hash
            2. If that element is not seen in A array, move to @Aonly


a) Straightforward Implementation:   
            %seen = ( );
            @Aonly = ( );

            foreach $item (@B) {
                        %seen{$item} = 1;
            }
           
            foreach $item (@A) {
                        unless ( $seen{$item} ) {
                                    push (@Aonly, $item);
                        }
            }

           
b) Merging the code:
           
            %seen = ( );
            @Aonly = ( );
           
            @seen{@B} = ( );          ## Marking the B array elements = undef. Below we are checking only the existance of the key not the logical truth or definedness of the value
           
            foreach $item (@A) {
                        push ( @Aonly, $item) unless ( exists $seen{$item} );    ## Move to Aonly array if the element is not matching with B array
            }

c) Loopless version:
            1. Mark all A array elements by creating %seen hash
            2. Delete the B array element if any from that hash
            3. print the keys of the hash ==> Aonly array elements.
           
            %seen = ( );
           
            @seen {@A} = ( );
            delete @seen {@B};
           
            @Aonly = keys %seen;

================================================================
11. Computing Intersection, Union, Difference of Unique lists A & B:

Algo:
            1. Mark all elements of array A by creating %seen hash.
            2. Traverse all the elements of array B.
                        - Mark the elements if already seen in list A and mark them by create isect hash.
                        - Continue marking if not seen in seen in list A.
                        - union = keys of %seen
                        - isect = keys of %isect

a) Straightforward solution:
            @A = (1,3,5,7.8) ;
            @B = (2,3,5,7,9);

            @union = @isect = @diff = ( );
            %union = %isect = ( );

            foreach $item (@A)  {
                        %seen{$item} = 1;
            }
           
            foreach $item (@B) {
                        if ( $seen{$item} ) {
                                    %isect{$item} = 1;
                        }
                        %seen{$item} = 1;
            }

            @union = keys %union;
            @isect = keys %isect;

b) More idiomatic version:
            1. The First time through it won't be in the union, which makes the first part of the && false,
                so the second part is consequently ignored.
            2. The second time that we encounter the same element, it's is already in the union, so we put in
                in the intersection.
           
            foreach $e (@A, @B)  {
                        $union {$e}++ && $isect{$e}++
            }

            @union = keys %union;
            @isect = keys %isect;



c) Another approach:
            1. Mark all elements by creating hash. Increment by 1 if occurs more than once.
            2. If it occurs more than once => put in the insection array otherwise put in the difference array.
            3. keys of above hash = > unique elements in two arrays => union array.
           
           
            %count = ( );
            @union = @diff = @isect = ( );  
            foreach $e ($a, $b)  {
                        $count{$e}++;
            }

            @union = keys %count;

            foreach $e (keys %count)  {
                        if ( $count{$e} > 1 ) {
                                    push (@isect, $e );
                        }  else  {
                                    push (@diff, $e);
                        }
            }

d) Using conditional operator (?:)

            foreach $e (keys %count)  {
                        push  @{ ($count{$e} >1) ? \@isect : \@diff }, $e;    ## array by reference
            }

================================================================
12. Using Splice function in the array:

Splicing Arrays:
            Useful when you want to copy and remove elements from the middle.

This process is called splicing and is handled by the splice function.
splice ARRAY, OFFSET, LENGTH, LIST
splice ARRAY, OFFSET, LENGTH
splice ARRAY, OFFSET

Return value:
            The return value in every case is the list of elements extracted/removed from the array in
the order that they appeared in the original.

Arguments:
            1. The first argument, ARRAY, is the array that you want to remove elements from.
            2. The second argument is the index number of ARRAY  that you want to start extracting elements from.
                        --> At which index, you want to do this remove the element.
            3. The LENGTH, if specified, removes that number of elements from the array. If you don’t specify LENGTH, it
                 removes all elements to the end of the array. If LENGTH is negative, it leaves that number of elements on the end of the array.
                        ---> How many elements you want to remove here.
            4. Finally, you can replace the elements removed with a different list of elements, using the values of LIST.
                        Note that this will replace any number of elements with the new LIST, irrespective of the number of elements removed or replaced. The array will
               shrink or grow as necessary.
                        ---> Do you want to replace the removed elements by list? If yes, specify the LIST.

Example:
For example, in the following code, the middle of the list of users is replaced with a new set, putting the removed users into a new list:

@users = qw/Bob Martin Phil Dave Alan Tracy/;
@newusers = qw/Helen Dan/;
@oldusers = splice @users, 1, 4, @newusers;
This sets @users to
             Bob Helen Dan Tracy
and @oldusers to
            Martin Phil Dave Alan

a) Removing $N elements from the front of @ARRAY:
            @FRONT = splice (@ARRAY, 0, $N);
                        --> This is equivalent to write shift(@ARRAY) $N times and assigning the shifted values to @FRONT array.

b) Removing $N elements from the end of @ARRAY:
            @END = splice (@ARRAY, 0, -$N);
                        ---> This is equivalent to writing pop(@ARRAY) $N times and assigning the poped values to @END array.

c) Direct Method                                              Splice Equivalent

  push (@A, $x, $y)                                            splice(@A, @A, 0, $x, $y);           ## second argument => index = at the end of the array
  pop (@A)                                                        splice (@A, -1)   OR    splice (@A, ($#@A - 1), 1)
  shift (@A)                                                       splice (@A, 0, 1)
  unshift(@A, $x, $y)                                          splice (@A, 0,0,$x,$y);
  $A[$x] = $y                                                     splice (@A, $x, 1, $y);
  @A = ( )                                                          splice (@A)



$ref = \@A;
@removed = shift2 ( @{$ref} );
sub shift2 (\@)  {                                               sub shift2 (\@)  {
            $a = shift (@{$_[0]});                                         return splice ( @{$_[0]}, 0, 2 );
            $b = shift (@{$_[0]});                             }
            return ($a,$b);
}

================================================================
13. Finding the First List Element that passes a Test:

a) Straightforward Approach:
            foreach $item (@list)  {
                        if ($item =~ /CRITERION/)  {
                                    $match = $item;
                                    $found = 1;
                                    last;
                        }
            }                      

b) Using built in functions:
            use List::Util qw (first);
            $match = first {CRITERION } @list;

================================================================
14. Finding all the elements in an array matching certain criteria:

a) Starightforward Approach:

            @matching = ( );
            foreach (@list)  {
                        push ( @matched, $_)  if  TEST ($_) ;

b)
            @matching = grep { TEST ($_)  } @list;
                                   

Example 1: Find the elements of Array ARRAY whose value is greater than 1000.

            @matching = grep { $_ > 1000 } @ARRAY;

Example 2:
            @SDF_array = grep { /sdf_cond/ } @LIB;

================================================================
15. Using "map" function in the arrays:
            The map function performs an expression or block expression on each element within a
list. This enables you to bulk modify a list without the need to explicitly use a loop.

Syntax:
            map EXPR, LIST
            map BLOCK LIST

Return value:
            The individual elements of the list are supplied to a locally scoped $_, and the
modified array is returned as a list to the caller.

Example:
1. Convertt all the elements of an array to lowercase:

            @lcarray = map { lc } @array;

It is equivalent to the following:
                        foreach (@array)
                        {                      
                                    push @lcarray,lc($_);
                        }

Note that because $_ is used to hold each element of the array, it can also modify
an array in place, so you don’t have to manually assign the modified array to a new
one.

However, this isn’t supported, so the actual results are not guaranteed. This is
especially true if you are modifying a list directly rather than a named array, such as:
@new = map {lc} keys %hash;

================================================================
15. Randomizing an array:

            use List::Util qw(shuffle);
            @array = shuffle (@array );


================================================================
LOOP CONTROL STATEMENTS:
================================================================
last   => break
next  => continue
exit  => exit



================================================================
HASHES:
================================================================
1. Adding an element to HASH.
            %HASH = ( );
            $HASH {$KEY} = $VALUE;

================================================================
2. Testing for the presence of a key in Hash:
           
            if (exists $HASH {$KEY})  {
                        ### it exists 
            }  else {
                        ## it doesn't
            }
================================================================
3. Deleting an element from HASH:

            delete ($HASH {$KEY} );

================================================================
4.  Creating a Hash with Immutable keys or values:
            Hash keys or values can't be altered once set.

================================================================
5.  Traversing a Hash:

a) Using while loop:
           
            while ( ($key, $value) = each (%HASH) )  {
                        ## print " $key => $value \n";    
            }

b) Using foreach loop:
           
            foreach $key (keys %HASH)  {
                        $value = $HASH{$key};
            }

            foreach $value (values %HASH)  {
                        ##
            }
c) Using map function:

            print map { $_ => $HASH{$_} }  (keys %HASH) ;

================================================================
6.  Hashes with Multiple values per Key:
            - how to store more than one value per key.

a) Solution:
            Store an array reference in $HASH{$KEY}, then put the values into the referenced array.
            The value associated with each key in the array is "Reference to Array"

            "file" is the output of the who command  in UNIX.
            ---------------------------------------------
            %ttys = ( );
           
            open (WHO, "
           
            while()  {
                        ($user, $tty)  = split;                  
                        push ( @{$ttys{$user}} , $tty );
            }
           
            foreach $user (sort keys %ttys)  {
                        print "$user:  @{$ttys{$user}}\n";
            }
           

================================================================
7.  Inverting a Hash:

%REVERSE = reverse %HASH;
================================================================
8.  Merging 2 Hashes:
            %merged = (%A, %B);
           
================================================================
9. Finding common & different keys in 2 hashes:
           
            @common = @different = ( );

            foreach  ($keys %HASH1)  {
                        push (@common, $_) if exists $HASH2{$_};
            }

            foreach  ($keys %HASH1)  {
                        push (@common, $_) unless exists $HASH2{$_};
            }
================================================================
10. (5.13 Hashing Referenes??)



                       
           
================================================================
PATTERN MATCHING:
================================================================
1. Copying & Substituting simultaneously:

Example 1:
$dst = $src;                               ===>  equivalent to     ($dst = $src) =~ s/this/that/ ;
$dst =~ s/this/that/;

Example 2: The above technique can be extended to arrays also.
            @bindirs = qw( /usr/bin /bin /usr/local/bin );        
            for (@libdirs = @bindirs)  { s/bin/lib }
           
            print "@libdirs\n";

Example 3:
 ($a = $b) =~ s/x/y/g;                 # copy $b and then change $a;
 $a = ($b =~  s/x/y/g) ;               # change $b and then assign to $a;
$a = $b =~ s/x/y/g;                     # same as 2. changes will happen for $a, $b at the same time.

2. Checking whether the word is purely alphabetic or not:

            if (/^[^\W\d_]+$/)  {
                        print "$_ : alphabetic \n";
            } else {
                        print "$_ : non-alphabet \n";
            }

3. Matching words:
            /\b([A-Za-z]+)\b/                        ### \b -> boundary of the word

4. Matching -ve or +ve floating or integers:

            /^\s*[-+]?\d+\.?\d*\s*/                 ## matches even -123.34 or -123 or +123 or 123.23

5. Finding the Nth occurence of a Match:

            $string = " one fish two fish red fish blue fish ";
            print " Enter N value:";
            $N = ;
           
            @colors = ($string =~ /^\s*(\w+)\s*fish\b/gi);                               (OR)                        $color = ($string =~ /^\s*(\w+)\s*fish\b/gi)[$N-1];
            $color = $colors[$N-1];

================================================================
6. Explore (6.8 Extracting a Range of lines):
================================================================


================================================================
FILES:
================================================================
1. Printing to Many Filehandles simultaneouly:

foreach  $FH (@file_handles)  {
            print FH $line;
}

(OR)

open (MANY, " | tee file1 file2 file3 > /dev/null ) or die $!;

print MANY "$line\n";
close (MANY):


2. Reading a File Backward by Line or paragraph:

Solution:  Read all the lines into an array, then process that array from the end of the start.

@lines =

while ($line = pop @lines)  {
            # do something here
}

@lines = reverse
foreach $line (@lines)  {
            # do something here
}

3. Removing last line of the file:

a) Using built-in functions:
            use Tie::File;
           
            tie @lines, Tie::File, $file or die "can't update $file: $!";
            delete $lines[-1];
           
b) Removing 'N' lines from the end of the file:
            @lines = ;
            pop(@lines);   #### Do this N times  OR use splice function here.

            foreach $line (@lines) {
                        print OUT "$line\n";
            }          
                       


================================================================
SubRoutines:
================================================================
1. Write a subroutine to trim the strings.
            - Removing leading & trailing spaces of the string.

2.  Understand the INIT block.
            - (Perl Cookbook - 10.3 section)
LOW lights:
            10.4 section

================================================================
3. Passing arrays and hashes by Reference:
Need:              
            You want to pass more than one array or hash and have each remain distinct.

sub add_array (@a, @b);
            The above statement passes 2 arrays as one array i.e. @_ = (@a, @b) and these
two can't be remain distinct.

Solution:
            Send the references of 2 arrays as arguments. Since each reference to an array is an scalar (address),
it can be treated in the subroutine as follows.

            sub add_array (\@a, \@b);

The above statement passes 2 arrays references as one array (i.e., @_ = (reference to array A, reference to array B).

You dereference these 2 to make it as distince arrays.

KEY MESSAGE:
            If you want to play with more than one array or hash in subroutines,  YOU SHOULD USE
REFERENCES ONLY.

================================================================
4. Write a subroutine to add 2 arrays of same size passed as arguments:
           
#!/usr/local/bin -w
use strict;

our (@A, @B,@c);

@A = [ 1, 3, 5];
@B = [2, 4, 6];
@c=add_2_arrays (\@A, \@B);

sub add_2_arrays   {
            our ($x, $y) = @_;
            my @result;
            my $i;

            for  ($i=0;  $i< @$x; $i++) {
                        $result[$i] = $x->[$i] + $y->[$i];
            }
   return (@result);
}

================================================================
4. Detecting Return Context:
            V. IMP:  Section 10.6

Solution:  Use wantarray() function.


================================================================
5.Passing by named parameter:
================================================================
6. Returning more than one hash or array

sub fun {

            return (\%A,  \%B,  \%C);   ==> equivalent to                   return  \(%A, %B, %C);
================================================================
7. Check programming perl book (chapter 29)

=================================================================
1.     If you want to replace set of words with other words. Create hash with the following
 key= present word, value = word to be replaced with.

Advantage: Without changing the code, you can add/remove other words.

Ex: Would like to change the Mega, Giga => 1e6, 1e9 etc

2.     Adacond changes:
1.     Input files:
a.     Synopsys .lib file
b.     Adaconds file
                                               i.                
                                              ii.     Ex:  “adacond_D”                    “D”
==================================================================
Classes, Objects:
An object is a variable that belongs to a class.
Methods are functions associated with a class.
In perl,
Class  ó Package or module (.pm file)
Object ó reference to something associated with a class

Once associated with a class, something is said to be blessed into that class.
Blessing merely associates a referent with a class, this is done using bless function.
Bless function takes 2 arguments.
1.     Reference to the thing you want to associated with the class. i.e., refence to an object
2.     Package with which to make the association.  i.e., package/module name
Example:
$object = { };                 ##hash reference to the object
bless($object, “Data::Encoder”);             ## create the object reference for Data::Encoder    class
bless($object);  ### bless generated $object into current package/module

key messages:
Generally, objects are implemented as blessed hash references as they allow arbitrarily named data fields in an object.
Purpose of blessing (Associating a package with a referent):
            Perl can determine the package  namespace in which to find functions when you invoke methods against an object.
Examples:
$encoded = Data::encoder->encode(“data”);    
(OR)
$object = { };
Bless($object, “Data::encoder”);
Bless($object);
$encoded = $object -> encode(“data”);
Here,
$object is called method’s invocant. (A invocant as the entity on whose behalf the method is called)
Method name: encode

Invoking a method calls the function in the corresponding class, implicitly passing its invocant as the initial argument to that function.
Constructor methods:
            Most classes provide constructor methods, which return new objects.
Example:
$object = Classname->new();
Sub new {
            My $class = shift;
            My $selft = { };
            Bless ($self, $class);
            Return ($self);
}
The first argument to new function:  Name of the class i.e., $class = Classname
==================================================================
13.1  Constructing an Object:
            Constructor is a method that allocates and initializes a new object.
Reference is not an object unless bless has been called on it.

Inherit = acquire, get, have, receive
  1. Obtain from someone after their death.
    1. I inherited a castle (palace) from my French grand parents
  2. Receive from a predecessor.
    1. The new chairman inherited many problems from the previous chair.
  3. Receive by genetic transmission
    1. I inherited my good eyesight from my mother.

Inheritance means allowing one class to piggyback on the top of another so you don’t have to write the same code again and again
Piggyback = ride on someone’s shoulder or back
-        She carried her child piggyback.
Heed = pay the close attention
-        Heed the advice of the old man.
How the constructor is inherited by a derived class:
Have the constructor heed its first argument.
-        For a class method, this is the package name. Pass the class name as the second argument
to bless.
Example:
### Calling the constructor
            $object = Classname->new();
### Typical constructor used as a class method looks like this
            sub new   {
                        my $classname = shift;  ## what class we are constructing??
                        my $self            = { };     ##Allocate new memory => defining anonymous hash
bless ($self, $classname);          ### reference has become object and marking it of   type   $classname
###Initialization of data fields
$self->{START} = time();
$self->{AGE} = 0;
####Returning the constructed object
Return $self;
     }
==================================================================
Example2: Separate the memory allocation and blessing step from the instance data initialization step.

sub  new  {
            my $classname = shift;
            my $self = { };
            bless($self, $classname);
            $self->_init(@_);
            return $self;
}
sub _init  {
            my $self = shift;             ### implicit argument = object name
            $self->{START} = time();
            $self->{AGE} = 0;
###If any other data fields are passed as arguments, then
            if(@_) {
                        my %extra = @_;
                        @self{keys %extra} = values %extra;
            }
}




my %dramatis_personae = (
    humans => [ 'hamnet', 'shakespeare', 'robyn', ],
    faeries => [ 'oberon', 'titania', 'puck', ],
    other => [ 'morpheus, lord of dreams' ],
);
Access it like this:
foreach my $group (keys %dramatis_personae) {
    print "The members of $group are\n";
    foreach (@{$dramatis_personae{$group}}) {
        print "\t$_\n";
    }
}





$value = $expr ? $true : $false;
Size of an array:
Difference b/w $#array and scalar @array:
$#array -> gives the maximum index of the array
Scalar @array -> size of the array (max index + 1)

The size of an array can be determined using scalar context on the array—the returned
value will be the number of elements in the array:
===========================================================================
@array = (1,2,3);
print "Size: ",scalar @array,"\n";
The value returned will always be the physical size of the array, not the number of
valid elements. You can demonstrate this, and the difference between scalar @array
and $#array, using this fragment:
@array = (1,2,3);
$array[50] = 4;
print "Size: ",scalar @array,"\n";
print "Max Index: ", $#array,"\n";
===========================================================================
This should return
Size: 51
Max Index: 50

Printing the hash:
foreach $key (%ages)
{
print "$key is $ages{$key} years old\n";
}
while (($key, $value) = each %ages)
{
print "$key is $ages{$key} years old\n";
}

Checking for existence:
if (exists($ages{$name}))
{
print "$name if $ages{$name} years old\n";
}
else
{
print "I don't know the age of $name\n";
}
Size of the hash:
You get the size—that is, the number of elements—from a hash by using scalar context on either keys or values:
print "Hash size: ",scalar keys %hash,"\n";

Selecting List Elements from Function Calls
We can even use list notation on the return value from a function call. For example, the
localtime function returns a list of time values (hours, minutes, days, and so on), and
we can extract just the elements we want:
($hours,$minutes) = (localtime())[2..3];
Note that the parentheses go around the expression that returns the list, to imply
list context on the overall expression. The following are all examples of how not to
extract individual elements from a function that returns a list:
$hours = localtime()[2];
$hours,$minutes = localtime()[2..3];
($hours,$minutes) = localtime()[2..3];
The defined Function and the Undefined Value
The undefined value, undef, is an alternative to the null value used in C. In essence,
undef means that the variable has had no value assigned. This is useful if you want to
create an undefined variable—one that has no value. Compare the undefined value with
an integer with a value of 0 or an empty string, both of which indicate valid values.
The undefined value will always evaluate to false if used in an expression, for
example the test in this fragment:
$value = undef;
if ($value)
{
...
will always fail. It will also raise an error because you’ve tried to access the contents of
an undefined value. In these situations, you can use the defined function to check the
value of a scalar. The defined function returns true if the scalar contains a valid value,
or false if the scalar contains undef:
if (defined($value))
{
...
Just to confuse you, defined will return false if a variable has never been named or
created, and also false if the variable does exist but has the undef value.
Note that the same rules apply to the scalar components of arrays or hashes: they
can contain the undefined value, even though the index or key is valid. This can cause
problems if you only use defined on a hash element. For example:
$hash{one} = undef;
print "Defined!\n" if (defined($hash{one}));
print "Exists!\n" if (defined($hash{one}));
This will only print “Exists!,” since the element’s value remains undefined.

Special variables:
Perl keeps an internal list of special variables that supply information and data about
the current scripts environment. The subsections that follow include standard variables
built into the interpreter, variables that have special meanings to core modules (such as
pragmas and Exporter), and also the special filehandles used for communicating with
the outside world.
Note that Perl uses a combination of special characters and names to refer to the
individual variables. To use the long (named) variables, you must include the English
module by placing
use English;

at the top of your program. By including this module, you arrange that the longer
names will be aliased to the shortened versions. Although there is no standard for
using either format, because the shortened versions are the default, you will see them
used more widely. See Web Appendix A for a listing of the variables and their English
module equivalents. The named examples are given here for reference.
Some of the variables also have equivalent methods that are supported by the IO::*
range of modules. The format of these method calls is method HANDLE EXPR (you
can also use HANDLE->method(EXPR)), where HANDLE is the filehandle you want
the change to apply to, and EXPR is the value to be supplied to the method.
_ (underscore) The underscore represents the special filehandle used to cache
information from the last successful stat, lstat, or file test operator.

$0
$PROGRAM_NAME The name of the file containing the script currently being
executed.
$1..$xx The numbered variables $1, $2, and so on are the variables used to hold the
contents of group matches both inside and outside of regular expressions.
$_
$ARG The $_ and $ARG variables represent the default input and pattern searching
spaces. For many functions and operations, if no specific variable is specified, the
default input space will be used. For example,
$_ = "Hello World\n";
print;
would print the “Hello World” message. The same variable is also used in regular
expression substitution and pattern matches.
Perl will automatically use the default space in the following situations even if you
do not specify it:
_ Unary functions, such as ord and int.
_ All file tests except -t, which defaults to STDIN.
_ Most of the functions that support lists as arguments (see Appendix A).
_ The pattern matching operations, m//, s///, and tr///, when used without an
=~ operator.
_ The default iterator variable in a for or foreach loop, if no other variable
is supplied.
_ The implicit operator in map and grep functions.
_ The default place to store an input record when reading from a filehandle.
$&
$MATCH The string matched by the last successful pattern match.
$`
$PREMATCH The string preceding the information matched by the last pattern match.
$’
$POSTMATCH The string following the information matched by the last pattern match.
$+
$LAST_PARENT_MATCH The last bracket match by the last regular expression
search pattern.
$* Set to 1 to do multiline pattern matching within a string. The default value is 0. The
use of this variable has been superseded by the /s and /m modifiers to regular expressions.
$.
$NR
$INPUT_LINE_NUMBER The current input line number of the last file from which
you read. This can be either the keyboard or an external file or other filehandle (such as
a network socket). Note that it’s based not on what the real lines are, but more what the
number of the last record was according to the setting of the $/ variable.
$/
$RS
$INPUT_RECORD_SEPARATOR The current input record separator. This is
newline by default, but it can be set to any string to enable you to read in delimited
text files that use one or more special characters to separate the records. You can also
undefine the variable, which will allow you to read in an entire file, although this is
best done using local within a block:
{
local $/;
$file = ;
}
@ISA The array that contains a list of other packages to look through when a method
call on an object cannot be found within the current package. The @ISA array is used
as the list of base classes for the current package.
$|
$AUTOFLUSH
$OUTPUT_AUTOFLUSH
autoflush HANDLE EXPR By default all output is buffered (providing the OS
supports it). This means all information to be written is stored temporarily in memory
and periodically flushed, and the value of $| is set to zero. If it is set to non-zero, the
filehandle (current, or specified) will be automatically flushed after each write operation.
It has no effect on input buffering.
$,
$OFS
$OUTPUT_FIELD_SEPARATOR The default output separator for the print series of
functions. By default, print outputs the comma-separated fields you specify without
any delimiter. You can set this variable to commas, tabs, or any other value to insert a
different delimiter.
$\
$ORS
$OUTPUT_RECORD_SEPARATOR The default output record separator. Ordinarily,
print outputs individual records without a standard separator, and no trailing newline
or other record separator is output. If you set this value, then the string will be appended
to the end of every print statement.
%OVERLOAD Set by the overload pragma to implement operator overloading.
$!
$ERRNO
$OS_ERROR Returns the error number or error string of the last system call
operation. This is equivalent to the errno value and can be used to print the error
number or error string when a particular system or function call has failed.
$[ The index of the first element in an array or of the first character in a substring.
The default is zero, but this can be set to any value. In general, this is useful only when
emulating awk, since functions and other constructs can emulate the same functionality.
$]
$OLD_PERL_VERSION The old version + patchlevel/1000 of the Perl interpreter.
This can be used to determine the version number of Perl being used, and therefore
what functions and capabilities the current interpreter supports. The $^V variable
holds a UTF-8 representation of the current Perl version.
$a The variable used by the sort function to hold the first of each pair of values being
compared. The variable is actually a reference to the real variable so that you can modify
it, but you shouldn’t—see Chapter 8 for information on usage.
@_
@ARG Within a subroutine (or function), the @_ array contains the list of parameters
supplied to the function.
ARGV The special filehandle that iterates over command line filenames in @ARGV.
Most frequently called using the null filehandle in the angle operator <>.
$ARGV The name of the current file when reading from the default filehandle <>.
@ARGV The @ARGV array contains the list of the command line arguments
supplied to the script. Note that the first value, at index zero, is the first argument,
not the name of the script.
ARGVOUT The special filehandle used to send output to a new file when processing
the ARGV filehandle under the -i switch.
$b The variable supplied as the second value to compare when using sort, along
with the $a variable.

$?
$CHILD_ERROR The status returned by the last external command (via backticks
or system) or the last pipe close. This is the value returned by wait, so the true return
value is $? >> 8, and $? & 127 is the number of the signal received by the process, if
appropriate.
%ENV The list of variables as supplied by the current environment. The key is the
name of the environment variable, and the corresponding value is the variable’s value.
Setting a value in the hash changes the environment variable for child processes.
@EXPORT The list of functions and variables to be exported as normal from a
module when using the standard Exporter module.
@INC The list of directories that Perl should examine when importing modules via
the do, require, or use construct.
%INC Contains a list of the files that have been included via do, require, or use. The
key is the file you specified, and the value is the actual location of the imported file.
$^P
$PERLDB The internal variable used for enabling the Perl debugger.
goto
BASIC programmers will be immediately happy when they realize that Perl has a goto
statement. For purists, goto is a bad idea, and in many cases it is actually a dangerous
option when subroutines and functions are available. There are three basic forms: goto
LABEL, goto EXPR, and goto &NAME.
In each case, execution is moved from the current location to the destination. In the
case of goto LABEL, execution stops at the current point and resumes at the point of
the label specified. It cannot be used to jump to a point inside a block that needs
initialization, such as a subroutine or loop. However, it can be used to jump to any
other point within the current or parent block, including jumping out of subroutines.
As has already been stated, the use of goto should be avoided, as there are generally
much better ways to achieve what you want. It is always possible to use a control flow
statement (next, redo, etc.), function, or subroutine to achieve the same result without
any of the dangers.
The second form is essentially just an extended form of goto LABEL. Perl expects
the expression to evaluate dynamically at execution time to a label by name. This
allows for computed gotos similar to those available in FORTRAN, but like goto
LABEL, its use is deprecated.
The goto &NAME statement is more complex. It allows you to replace the
currently executing subroutine with a call to the specified subroutine instead.
This allows you to automatically call a different subroutine based on the current
environment and is used by the autoload mechanism (see the Autoload module in
Appendix B) to dynamically select alternative routines. The statement works such
that even the caller will be unable to tell whether the requested subroutine or the
one specified by goto was executed first.



Subroutines:
sub add
{
my $result;
while(@_)
{
$result += shift;
}
print "Result: $result\n";
}
Now we can call the function with any number of arguments,
add(1);
add(1,2);
add(1,2,3,4,5,6,7,8,9,10,11);
Or a list:
add(@values);

Dispalying hash:
sub display_hash
{
my (%hash) = @_;
foreach (%hash)
{
print "$_ => $hash{$_}\n";
}
}

Returning value:
sub myfunc
{
if (@_)
{
return $_[0]+$_[1];
}
else
{
return 0;
}
}
When called, return immediately terminates the current subroutine and returns the
value to the caller—if you don’t specify a value then the return value is undef.
Special Blocks:
Perl has reserved a number of specially named blocks that provide some additional
control over the execution of your script—although these are more complex topics,
we’ll cover them here, as their execution will help you to understand how modules
and importing and exporting objects works.
The four blocks are BEGIN, CHECK, INIT, and END, and they are executed in
that order. When you execute a Perl script, any BEGIN blocks are executed during
the parsing process—that is, as soon as the statements within a BEGIN block have
been parsed and verified. The CHECK block is executed as soon as the parsing and
compilation stages have been completed, but before the actual execution of the script.
The INIT block runs before the main flow of the program starts. The END blocks execute
when the program terminates.
If you specify more than one of these blocks in your script, they are executed in the
order in which they are parsed in the case of BEGIN and CHECK, and in reverse order
in the case of INIT and END, and still in the overall order given above. You can see
this better using a simple script:
print "Now in the main script\n";
die "Script abnormally terminated!\n";
CHECK { print "1st declared CHECK block\n" }
CHECK { print "2nd declared CHECK block\n" }
END { print "1st declared END block\n" }
BEGIN { print "1st declared BEGIN block\n" }
INIT { print "1st declared INIT block\n" }
BEGIN { print "2nd declared BEGIN block\n" }
END { print "2nd declared END block\n" }
INIT { print "2nd declared INIT block\n" }
When executed, the script generates the following:
1st declared BEGIN block
2nd declared BEGIN block
2nd declared CHECK block
1st declared CHECK block
1st declared INIT block
2nd declared INIT block
Now in the main script
Script abnormally terminated!
2nd declared END block
1st declared END block
Note that the execution also applies to individual packages and modules. Here, the
BEGIN and END blocks can act as initializers and finalizers for the package. They are
defined like this:
BEGIN { print "Start!\n" };
END { print "End!\n" };
A BEGIN block is executed as soon as possible after it has been defined. This overrides
the parsing of the rest of the package. You can have multiple BEGIN blocks that
are executed in the order they were defined. You can use a BEGIN block to import
functions and values from other modules so that the objects required by the rest of
the package are defined at the point the block is parsed. This can be especially useful
if you are using the function prototyping and declarations seen earlier in this chapter.
If a function has been defined such that it is interpreted as an operator, or with a specific
prototyping format, then it will need to exist before Perl interprets the rest of the package.
An END routine is the opposite: it is executed as late as possible. In practice, this
means that an END block is executed at the point the parser and interpreter are about
to exit to the calling process. This is the case, even if the reason for the failure is a die
function or the result of an exception raised due to the nonexistence of a required system
call. You can use this facility to help print error messages or close filehandles cleanly in
the event of an error. Of course, in a well-written Perl script, you should be able to find
cleaner ways of handling exceptions and errors.
END blocks are executed in reverse order—that is, the last END block specified
will be the first to be executed. The following program doesn’t do quite what we want,
although it’s pretty close:
BEGIN { print "Eanie\n" }
die "Meanie\n";
END { print "Miney\n" }
END { print "Mo\n" }
You should not assume that the main program code has been executed in an END
block. Care is needed to ensure you don’t try to use a variable or function in an END
block that has not otherwise been defined, although you should be doing this kind of
checking in the main body of the script anyway.
FILES:
while(<>)
{
if(eof())
{
print "Running out of data!!\n";
}
...
}