Sunday, July 17, 2011

if statement and syntax perl

if statement:
If statement is mainly used by for decision making.
There are actually 3 ways it can be used.
Different Syntax which can be used:

1)

if (CONDITION) {
#code block to be executed
}
2)

if (CONDITION) {
#code block to be executed
}
else {
#code block to be executed if condition fails
}
3)

if (CONDITION_ONE) {
# code block to be executed if CONDITION_ONE passes
}
elsif (CONDITION_TWO)
{
#code block to be executed if CONDITION_TWO passes and CONDITION_ONE FAILS
}
else
{
# code block to be executed if both conditions fails
}
Example:

1)

$number=10;
if ( $number = 10 )
{
print "The number is 10";
}

Output:
The number is 10

2)

$number=20;

if ( $number = 10 )
{
print "The number is 10";
}
else
{
print "The number is not 10";
}
Output:
The number is not 10

3)

$number=20;
if ( $number = 10 )
{
print "The number is 10";
}
elsif ($number =20 )
{
print "The number is 20";
}

else
{
print "the number is not 10 and 20";
}

Output:
The number is 20;


To view the file information perl

To view the file information for a file in perl use the following statement:

Content of the Perl File:
use POSIX qw(strftime);
use File::stat;
my $sb = stat("test.txt");
print "Size: ".$sb->size."\n";
print "mtime in unreadable format:". $sb->mtime."\n";
$mtime=$sb->mtime;
my @systime =  localtime($mtime) ;
 my $str = strftime( "%a %b %e %H:%M:%S %Y", @systime );
print "mtime in readable format:".$str."\n";
 print "chmmod :".$sb->mode ."\n";
printf "Permissions are %04o\n", $sb->mode & 07777;
print "ctime :". $sb->ctime."\n";
 #ctime can also be converted into correct format similarly as did for mtime

We can access all the other file attributes given below by using $sb->needed file attribute as done for attribtes like mtime,ctime.

  1. 0 dev device number of filesystem
  2. 1 ino inode number
  3. 2 mode file mode (type and permissions)
  4. 3 nlink number of (hard) links to the file
  5. 4 uid numeric user ID of file's owner
  6. 5 gid numeric group ID of file's owner
  7. 6 rdev the device identifier (special files only)
  8. 7 size total size of file, in bytes
  9. 8 atime last access time in seconds since the epoch
  10. 9 mtime last modify time in seconds since the epoch
  11. 10 ctime inode change time in seconds since the epoch (*)
  12. 11 blksize preferred block size for file system I/O
  13. 12 blocks actual number of blocks allocated

This was reffered from http://perldoc.perl.org/functions/stat.html

Output :

G:\perl_programs>perl perl_size.pl
Size: 42
mtime in unreadable format:1310916921
mtime in readable format:Sun Jul  21:05:21 2011
chmmod :33206
Permissions are 0666
ctime :1310916921

G:\perl_programs>

Read and write in the same file perl

Content of hello.dat file  before running the command:

hello how are you
wonderful beautiful
great
award great
awesome

Content of the perl file:

open(FILE,"+<hello.dat");
# "+<"  option has to be used for read and writing to the file
@lines=<FILE>;  
foreach(@lines)
{
chop;
print;   # it is same as print $_;
print "\n";
}
print FILE "\n"."karthik";
close(FILE);



Output Execution:
G:\perl_programs>perl perl_file.pl
hello how are you
wonderful beautiful
great
award great
awesom

G:\perl_programs>

Content of the hello.dat file after execution:

hello how are you
wonderful beautiful
great
award great
awesome
karthik

To repeat words in perl

This makes use of the repeatation operator "x" to repeat the words

Content of the perl file:

$dd="hello";
$ss="$dd "x 5;
print $ss;

Output:

G:\perl_programs>perl perl_repeat.pl
hello hello hello hello hello
G:\perl_programs>

To find the filesize in perl

Solution 1:
For example for file test.txt.It is done by using the stat function in File module

use File::stat;
my $filesize = stat("test.txt")->size;
print "Size: $filesize\n";

Output:

Size: 42

Solution 2 :
It is done using the -s option

my $dd= -s "test.txt";
print "Size : $dd";

Output:

Size: 42

Call by reference perl

When you call by reference ,you use directly the scalar reference and so any change in the function causes the change to happen in the main program.

Content of the perl file:

@array=("hello","aaaa","lll","ggg");
print "The content of array element 0 and 1 before function call is $array[0] and $array[1]"."\n";
function(@array);
print "The content of array element 0 and 1 after function call is $array[0] and $array[1]";
sub function()
{
$_[0]="Great";#this is the scalar reference of the first element in the array.
$_[1]="awesome";#this is the scalar reference of the second element in the array.
}

Output:

G:\perl_programs>perl perl_pass_by_reference.pl
The content of array element 0 and 1 befoe function call is hello and aaaa
The content of array element 0 and 1 after function call is Great and awesome
G:\perl_programs>

finding no of array elements perl

Content of the perl file:

@array=("hello","aaaa","lll","ggg");
print "The no of elements in the array is ".scalar(@array)."\n";
#it can also be done by this way
$no=@array;
print "The no of elements in array is ". $no;

Output:

G:\perl_programs>perl perl_array_no.pl
The no of elements in the array is 4
The no of elements in array is 4


G:\perl_programs>