Saturday, 19 April 2014

perl + object oriented + word grep

##############  Package #############################

use warnings;

package file22;    -> Package creation

sub new                -> Constructor creation
    {
    $class=shift;
    $self={
        filename=>shift,
        word=>shift,
        };
    return bless $self, $class; -> Returning self
    }


sub files   
    {
        $self=shift;
    $filenames=$self->{filename};
        open("FILE","$filenames");
    @sam=<FILE>;
    return @sam;
    }

sub grep
    {
    $self=shift;
    @loop=shift;
    $words=$self->{word};
        foreach $i (@loop)
        {
            if ( $i =~ /$words/ )
            {
            push (@gather,$i);
            }
        }
    return @gather;
    }
1;


############# Perl script ###########################

use warnings;

use file22;

$s=file22->new('/etc/passwd',"bin");
@w=$s->files();
@all=$s->grep("@w");
print $_."\n" foreach (@all);

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









No comments:

Post a Comment