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############################



No comments:

Post a Comment