Wednesday 14 August 2019

Thanks viewers!!!

 


What is the use of @INC and how many methods to change it in Perl?


Deafult @INC is constructed with binaries, which can be listed using "env -i perl -V" and all thease binaries will be placed under common directory, for the binary compilation of our custom modules so we need to add the path for execution as well as compilation
below commands adds the path with @INC

To list Environmental variable PERL5LIB (or PERLLIB):perl -v

Method1:

perl -I /my/moduledir your_script.pl

Method2:

#!/usr/local/bin/perl -w -I /my/moduledir

Method3:

use lib ("/dir1", "/dir2");

Method4:

command line:
perl -Mlib=/dir1,/dir2

Method5:

unshift @INC, $dir,push @INC, $dir



kaavannan perl blogspot

Labels: , , , , , , ,

Tuesday 13 August 2019

How to find size of an array in Perl and what are the ways to find it?


Method1:
@a=(1,2,9,5,6);
print scalar @arr;


Method2:
@a=(1,2,9,5,6);
print $#a;   
print $#a + 1;


Method3:
perl -le '@a=(1,2,9,5,6); print $#a + 1;'


Method4:
my $size = @arr;
print $size;


Method 5:
@a=(1,2,9,5,6);
print @a."\n";


Method 6:
@a=(1,2,9,5,6);
print scalar grep /$_/ ,@a;



Method 7:
@a=(1,2,9,5,6);
print scalar map /$_/ ,@a;


Method 8:

@a=(1,2,9,5,6);
print scalar map $_ =~ s///,@a;


Method 9:

@a=(1,2,9,5,6,8);
print scalar grep $_ =~ s///,@a;

Method 10:

@a=(1,2,9,5,6,8);
print $_= map{$_ =~ s///}@a;

Method 11:

@a=(1,2,9,5,6,8);
print  scalar grep{$_ =~ s///}@a;


Method 12:

@a=(1,2,9,5,6,8);
$b=0;
$b+=$_=~s/// for @a;
print $b;




Wednesday 7 August 2019

How to search and replace on multiple lines in multiple methods using perl?

 
Method1:

perl -i -pe 's/START.*STOP/replace_string/g' file_to_change

Method2:
 
perl -i -pe 'BEGIN{undef $/;} s/START.*STOP/replace_string/smg' file_to_change

Method3:
 
perl -0pe 's/search/replace/gms' file

Method4:
 
local $/ = undef;
   open INFILE, $full_file_path or die "Could not open file. $!";
   $string =  <INFILE>;
   close INFILE;

Tuesday 6 August 2019

Can't locate Foo.pm in @INC, install it using cpan and solve it in many methods?


Method1:
cpan File::Name

Method2:
in windows: 
ppm
ppm> search net-smtp
ppm> install Net-SMTP-Multipart

Method3:
sudo perl -MCPAN -e 'install Foo'

Method4:
start cpan in your shell:
# cpan
and type
install File::Name

Monday 5 August 2019

How to pass command line Arguments to a perl script using multiple methods?


Method1:

perl tmp.pl 1 2 7 3 5
($a,$bcsa,$msx, $flag ,$key) = @ARGV;

Method2:

while (my $var = <>) {
  print $var;
}

Method3:

while (my $var1 = shift) {
  print "$var1\n";
}

Method6:
foreach my $argnum (0 .. $#ARGV) {

   print "$ARGV[$argnum]\n";

}

Method7:
foreach my $arg (@ARGV) {
    print $arg, "\n";
}

Method5:

If the arguments are options/switches, use GetOpt::Std or GetOpt::Long,
If they have a different meaning, you can use GetOpt::Std and GetOpt::Long to process them easily.

 GetOpt::Std supports only single-character switches and GetOpt::Long is much more flexible. From GetOpt::Long:

use Getopt::Long;
my $data   = "file.dat";
my $length = 24;
my $verbose;
$result = GetOptions ("length=i" => \$length,    # numeric
                    "file=s"   => \$data,      # string
                    "verbose"  => \$verbose);  # flag
Alternatively, @ARGV is a special variable that contains all the command line arguments. $ARGV[0] is the first (ie. "string1" in your case) and $ARGV[1] is the second argument. You don't need a special module to access @ARGV.

kaavannan perl blogspot

Labels: , , , ,

Sunday 4 August 2019

How to auto generate values for a hash by giving an array or list elements as hash keys in perl?



Method 1:

                       %hash = map {$_=>1} "letcrackperl"=~/./g;
                        print   %final;

Method 2:

                        %final = map { $_ => 1 } 1..5;
                        print  %final;

Method 3:

                        @array=(9,2,3,4,3,6);
                       %final = map { $_ => 1 } @array;
                        print  %final;

Method 4:

                       @array=(9,2,3,4,3,6);
                       @final{@array} = (1) x @array;
                        print  %final;

Method 5:

                       @array=(9,2,3,4,3,6);
                       %final = map { $_,1 } @array;
                        print  %final;

Method 6:

                       @array=(9,2,3,4,3,6);
                       %final = map { $_,1 } split(",",$line)
                        print  %final;