Monday, 10 March 2014

Creating a object in perl

####################Perl Modules #################################

#!/usr/bin/perl

package file8;                  -----> declaring class

sub new                            -----> Variable initiation
{
$class=shift;
$self={
filename=>shift,
word=>shift,
};
return bless $self, $class;
}

sub readi                        ----->  function
{
$line=shift;
if ( $line =~ /$self->{word}/ )
{
return $line;
}
}

sub filer
{
open(FILE,"$self->{filename}");
@s=<FILE>;
return @s;
}

1;
###############Perl Files#####################


#!/usr/bin/perl

use file8;

$s=file8->new("/etc/passwd","bash");    ---------------> creating object
@d=file8::filer;                                             -----> Calling filer function from file8 module
foreach (@d)
{
$s=file8::readi($_);                                      ----> Passing argument directly to function
print $s if ($s);
}


#####################Result ##################

root:x:0:0:root:/root:/bin/bash
git:x:1000:1001:sam,111,211,233,nothing:/home/git:/bin/bash
jenkins:x:119:128::/var/lib/jenkins:/bin/bash
couchdb:x:125:136:CouchDB Administrator,,,:/var/lib/couchdb:/bin/bash


#################Snap############################



Tuesday, 4 March 2014

Accessing a function from perl module

package john;

sub john_home
{
chomp;
$i=shift;
if ( $i eq "y" || $i eq "Y" )
{
$j="working";
return $j;
}
}

1;

########################################################




use john;


$d=john::john_home("y");


print "$d\n";'


########Result##################


working