Tuesday, June 26, 2012

Perl - Reading, Writing , Appending & Closing file

You cannot read and write a file at the same time. You have to tell Perl in the Open statement which of the three modes (reading , writing , or appending) .

In Perl version 4 and 5  perl looks at the first character or two filename you give it and determines the open mode from that.

To read the file: Precede the name with <.

To write a file: Precede with the name >.

To append to a file: Precede the name with >>.

 Syntax for the statement is:
open(HANDLE, MODE , FILENAME)

Example:
Open(INFILE, '<sample.txt'); # for reading
Open(INFILE, '>sample.txt'); # for writing
Open(INFILE , '>>sample.txt) # for appending
$file = '>sample.txt'; # Prepare for writing
    
Closing the file name    
When you are done with reading or writing to a file handle, you need to close it. Perl closes all your file handles for you. To close the file handle, use the close function like this:

close FILE;  

Thursday, June 21, 2012

Perl - Garbage collection and Destructors

Automatic reclamation process is known as Garbage collection. Perl uses a fast and simple reference-based garbage collector. It does this automatically for you as soon as object goes out of scope.

You may want to provide your own destruction mechanism. For that you may need to define a special method called Destroy. This method will be called on the object just before Perl frees memory allocated to it.

A Destroy method is absolutely essemtial in the situations in which you have objects that refer to nested structures. Perl also uses only one Destroy method per object destroyed regardless of inheritance. If your class overrides a superclass's destructor, then your Destroy method may need to invoke the Destroy method for any applicable base classes

Perl Functions & Subroutines:

Below are some of the perl functions:

Function name                                                     Description
abs                                                                        returns the absolute value of the argument

alarm                                                                    alarm(seconds)
                                                                             Sends a Sigalarm signal after a number of seconds

atan2                                                                    atan2< X, Y>
                                                                            returns the arctangent of X/Y in the range <pi>.

chmod                                                                 chmod(MODE , LIST)

                                                                            changes the permissions list of files
dump                                                                   causes an immediate binary  image core dump.
many more......

Subroutines:
In addition to above inbuilt functions in perl , you can create your own subroutines or functions. Subroutines are the self-contained units of program designed to accomplish a specified task.

Subroutine declaration:
       sub subroutine_name;

Subroutine_definition:
      sub subroutine_name {Block}

NOTE:Subroutines can be defined anywhere in your program or even in another file.

Subroutine Call:
      do subroutine_name;
      &subroutine_name;
      subroutine_name();
      subroutine_name

Subroutine call with parameter:
      &subroutine_name (parameter1, paramter2,......)
      subroutine_name(parameter1, parameter2,......)

Tuesday, June 19, 2012

Perl – Manipulation of Strings

Concatenation of strings:
Example 1: Print “win”.”dow”   // It will print out “window”
 Example 2: $name = “sussy”
$sentence = “ask”.$name.”aout the job.”

Multiple strings display:
Print ‘w’x10
It will display 10 w in row
wwwwwwwwww

To display $ in the string:
Print “\$hello how r u”
It will display a:
$hello how r u