How to upgrade Perl

First you should upgrade Perl only if extremely necessary. And if you need to upgrade Perl for only 1 or 2 Modules which are not compatible then do this

(assuming you are using cpan)

cpanm Module::Name –prompt

(When it says incompatible version type Yes and go ahead. Most of the time it would work).

But if you still want to upgrade then here are the steps

1) Make sure you have a backup copy of before doing this. Just in case

2) Once you get to the cpan> prompt, type “cpan upgrade” to make sure you’re on the latest version.

3) Then, to upgrade Perl, just type “upgrade”. This will take a while (5-10 minutes) but assuming it succeeds, you’ll be onĀ  latest version of PerlĀ  with all of the new features.

P.S: It would not work on a MAC as it does not make any actual use of perl.

 

 

 

 

Advertisements

Perl unique array elements

open(INFILE, “$ARGV[0]”) or die(“Cannot open file $_[0] for reading: $!”);

while(my $line = <INFILE>) {
chomp($line);

my %hash = ();
my $v,$j;
my @list;
my @word = split(/,/,$line);

foreach(@word){
if(exists $hash{$_}){
$hash{$_}++;
}else{
$hash{$_}=1;
}
}

while(($v,$j) = each(%hash)){

push(@list,$v);

}

print join(“,”,sort(@list)).”\n”;
}

Perl mod function

open(INFILE, “$ARGV[0]”) or die(“Cannot open file $_[0] for reading: $!”);

while(my $line = <INFILE>) {
chomp($line);
my @arguments = split(/,/,$line);
my $i = $arguments[1] ;
my $mul = $i;
my $j = $arguments[0];
my $subtract = 0;
my $done = 0;
$count = 2;

if($j < $i){
print $j.”\n”;
$done = 1;
}elsif($i eq $j){
print “0”.”\n”;
$done = 1;
}

while($done eq 0){
while($i <= $j){
$subtract = $i;
$i = $mul * $count;
$count++;
}
my $remainder = $j – $subtract;
if($remainder < $j){
print $remainder.”\n”;
$done = 1;
}else{
$j = $remainder;

}

}

}

close(INFILE);