Thursday, October 30, 2008

How to read a file in PERL

The open() function

Data files are opened in Perl using the open() function. When you open a data file, all you have to do is specify (a) a file handle and (b) the name of the file you want to read from.

As an example, suppose you need to read some data from a file named "checkbook.txt". Here's a simple open statement that opens the checkbook file for read access:

open (CHECKBOOK, "checkbook.txt");

*** Use complete filepath. Generally its a good practice. Replace single slash with double slashes in file-path

In this example, the name "CHECKBOOK" is the file handle that you'll use later when reading from the checkbook.txt data file. Any time you want to read data from the checkbook file, just use the file handle named "CHECKBOOK".


Example - opening a data file and reading from it

Now that we've opened the checkbook file, we'd like to be able to read what's in it. Here's how to read one line of data from the checkbook file:

$record = ;

Pretty simple, eh? After this statement is executed, the variable $record contains the contents of the first line of the checkbook file. The "<>" symbol is called the line reading operator, and in this example we've put the checkbook file handle in the line reading operator, indicating that we'd like to read a line from the checkbook file.

Of course, instead of reading just one line of data, you may want to operate on many lines of data in the checkbook file. Suppose, for example, you wanted to print every record of information from the checkbook file. Here's the code to (a) open the checkbook file, (b) print each record from the file, and (c) close the file when you're finished working with it:

   open (CHECKBOOK, "checkbook.txt");

while ($record = ) {
print $record;
}

close(CHECKBOOK);

Notice the use of the close() statement in this example. You always want to close a file when you're finished reading from it, and since the while loop reads through the entire file, there's not much else to do when you're finished except close it.


Wednesday, October 29, 2008

Operators in PERL

I assume that examples used will explain about the usage of operators here.

1. Equality Operators :


Strings:
if ($Name eq "Ripple") { print "My Name is $Name" }
if ($Name ne "Ripple") { print "My Name is not $Name" }

Of course it would make more sense to use else here, but for the sake of examples be happy with this solution.

Numbers:
To compare numbers, use <, <=, >, >=, <> and ==.



2. If...then...else...elsif

$MyAge = 24

if ($MyAge < 18)
{ print "My age is less than 18" }
elsif ($number < 25)
{ print "My age is less than 25" }
else { print "My age is greater than 25" }

OUTPUT:
My age is less than 25

Simpler way to right If statement:

$Name = "Ripple";
print "My Name is Ripple" if $Name eq "Ripple";

Another way to use if-else:

$Size eq "Big" ? $output="BIG" : $output="small";
print $output;

if it is true, the code after the question marked will be executed. If not, the code ofter the colon will be executed. So if your if block only needs to do one statement, use "? :"



3. FOR loop...

for ($counter = 0; $counter <= 10; $counter++)
{ print $counter."," }
Output:
0,1,2,3,4,5,6,7,8,9,10,

$counter++ means, that $counter will be increased one by one and is short for $counter = $counter + 1. You can also use it with a minus, making it $counter- - . But you can do a little bit more while you are in a loop.

Place any of the commands into the loop and you con do this:

last Exit the loop immediately
redo Repeat the last run in the loop
next Proceed to the next run, don't run any command after this in the loop

The main use for those is, if you have to change the standard loop behaviour under special circumstances or rules, when you want to abort the loop and so on.

Some Basic things about PERL

1. PERL is Case sesitive...

2. What can I use Perl for?
Most people use PERL for either automating administrative tasks (write a .pl file and launch it with PERL.EXE) or in combination with a webserver to create dynamice web pages. Most UNIX based webservers have PERL support already integrated (like APACHE) whereas you have to install PERL on WIN32 platforms like Windows NT (IIS 4).

3. Variables are declared with a $

4. prompts for input and stores the value in the according variable

print "Your age please $firstname $lastname: ";
$myage = ;

5. How to define an array in Perl?

@myarray = ("red","green","black");

OR

@myarray = qw(red green black);

OR

$myarray[0] = "red";
$myarray[1] = "green";
$myarray[2] = "black";

6. Hashes in Perl:A Hash consists not only of onebut two values each - a KEY and a VALUE.
To initialize a hash write:
%myhash = ( "Name" => "Vijay Kumar Sharma",
"Hobby" => "Photography",
"Age" => 24
);

OR

$myhash{'Name'} = "Vijay Kumar Sharma";
$myhash{'Hobby'} = "Photography";
$myhash{'shoe size'} = 24;

*** Note Curly braces for hash keys...

foreach $key (keys %myhash) { print $key.": ".$myhash{$key}."\n"; } ==>> Ignore if not able to understand at this moment... I have to separately see how for loop works in PERL