[WpProQuiz 1]
[WpProQuiz_toplist 1]
Learning through sharing
[WpProQuiz 1]
[WpProQuiz_toplist 1]
If you are working on Perl and you need to work on modules which is listed in CPAN. You may need to download it, install it and use it in your box.
For that you need to know how to install Perl modules. This article will show you simple steps to download and install Perl modules in Linux box. I know its not the new topic for Perl chapter but I wrote it for our standalone repository on Perl and moreover, all these method with examples and pitfalls are not listed at many places. So, I think it will be worthy for our viewers and Perl mongers 😀
Method 1 using cpan shell(It has advantage over manual installation. Read Note section at the bottom)
1. perl -MCPAN -e shell
cpan> install HTML::Template
or
2. perl -MCPAN -e ‘install HTML::Template’
or
3. cpan -i MIME::Lite
Method 2 Manual module installation
if you’re having problems installing with CPAN. If you’re on the command line, you can use something like wget to grab the file. Next you’ll want to unzip it with something like:
tar -zxvf HTML-Template-2.8.tar.gz
In case above command doesnt work for you, try this
gzip -d XML-Parser-2.36.tar.gz
tar -xvf XML-Parser-2.36.tar
This will unzip the module into a directory, then you can move in and poke around – look for the README or INSTALL files.
In most cases, installing a module by hand is still pretty easy, though (although not as easy as CPAN). Once you’ve switched into the base directory for the module, you should be able to get it installed by typing:(I assume you are on Linux box)
1. perl Makefile.PL
2. make
3. make test
4. make install
If everything shows fine then Thats it. You are now ready to rock and roll with newly installed Perl modues 😀
But what if cpan command doesnt work for method 1?
1. Check first if cpan command works in your box
$cpan
cpan: Command not found.
If you get the above result means you need to install cpan module to work from CPAN otherwise, you have cpan installed in your system and you can skim next steps 😀
2. yum install perl-CPAN
3. after successful download and installation. Type cpan for the first time use
and type o conf commit on cpan prompt
then type quit on cpan prompt
You are done! Now Method 1 will work without fail.
Note: If any CPAN module is dependent on the several other modules. CPAN automatically resolves the dependencies and installs other dependent modules.
cpanm module is more advanced for doing all these without hassle. If possible give it a try.
[ADDED LATER]
As it was commented through Facebook by one of our readers, so I thought to better update this article with his comments.
# Install cpanminus (once).
curl -L http://cpanmin.us | perl – –self-upgrade or
cpan App::cpanminus
# Install your CPAN modules.
cpanm HTML::Template
cpanm XML::Parser
Perl is such a nice language that even a beginner can write the fully working code by using simpler things available in Perl. More you know about Perl more crazy you will be about it. Its very famous for one liner stuffs and even it has many functions which can minimize our code, run time and our work too!
Map function in Perl is a nice example for it. You can replace you whole foreach code with map in a single line.
Note: Block does not need comma but expression needs coma explicitly see the examples using { block } and (expression). In simple ,it is
For fully working code on map function please visit this link For more details on map click here
Sorting an array and hash elements in PerlStandard string comparison means based on ASCII value of those characters. Like @arr = qw (Call all). In this case it will be sorted as Call all which was not expected. So to make it work properly we use case-insensitive sort.
If SUBNAME is specified, it gives the name of a subroutine that returns an integer less than, equal to, or greater than 0 , depending on how the elements of the list are to be ordered. (The <=> and cmp operators are extremely useful in such routines.)
Note: The values to be compared are always passed by reference and should not be modified . $a and $b are global variable and should not be declared as lexical variables.
sort() returns aliases into the original list like grep, map etc which should be usually avoided for better readability.
As sorting always does string sorting, so to do numeric sorting we need to use a special syntax which a sort {$a $b} LIST. We will see these conditions using Perl codes.
How reverse sorting works
Systax to use reverse sort is reverse LIST. It works on sorted LIST usually. But in scalar context, it concatenates the elements of LIST and returns a string value with all characters in the opposite order.
In scalar context if argument is not passed it will reverse the value of $_
Ex:
[perl]$_ = “dlrow ,olleH”;
print scalar reverse; #in this case print reverse would not works because it expects a LIST
[/perl]
Try this out:
[perl]
my @arr1 = qw(1 two 3 0 4 Two 5 six 7 8 9 ten);
my @arr2 = sort {$a cmp $b} @arr1;
print “\n@arr2\n”;
[/perl]
Let go through the codes for different scenarios:
Example 1: Sorting an array of strings (case-sensitive and case-insensitive examples)
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my @strarr = qw(two Two six Six alien Coders Alien coderS);
my @sorted = sort {$a cmp $b} @strarr; # same result as of sort @strarr
my @sortedcase = sort { uc $a cmp uc $b } @strarr; #case-insensitivie
print “\n@sorted\n@sortedcase\n”;
[/perl]
Output:
Alien Coders Six Two alien coderS six two
alien Alien Coders coderS six Six two Two
Note: try to always use case insensitive for better string sorting results.
Example 2: Sorting an array of numbers
The Perl sort function sorts by strings instead of by numbers. If you do it in general way it would fetch unexpected result.
[perl] #!/usr/bin/perl
use strict;
use warnings;
my @numbers = (23, 1, 22, 7, 109, 9, 65, 3, 01, 001);
my @sorted_numbers = sort @numbers;
print “@sorted_numbers\n”;
[/perl]
The output you would see would be:
001 01 1 109 22 23 3 65 7 9
To sort numerically, declare your own sort block and use the binary equality operator i.e. flying saucer operator <=>:
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (23, 1, 22, 7, 109, 9, 65, 3, 01, 001);
my @sorted_numbers = sort {$a <=> $b} @numbers;
print “@sorted_numbers\n”;
[/perl]
The output would now be:
1 01 001 3 7 9 22 23 65 109
Note that $a and $b do not need to be declared, even with use strict on, because they are special sorting variables.
Example 3: Sorting array backwards (for string and numbers)
To sort backwards you need to declare your own sort block, and simply put $b before $a. or use reverse keyword after simple sort.
For example, the standard sort is as follows:
[perl] #!/usr/bin/perl
use strict;
use warnings;
my @strings = qw(Jassi Alien Coders);
my @sorted_strings = sort @strings;
print “@sorted_strings\n”;
[/perl]
The output would be:
Alien Coders Jassi
To do the same, but in reverse order:
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my @strings = qw(Jassi Alien Coders);
my @sorted_strings = sort {$b cmp $a} @strings; # or reverse sort @strings
print “@sorted_strings\n”;
[/perl]
The output is:
Jassi Coders Alien
And for numbers:
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (23, 1, 22, 7, 109, 9, 65, 3);
my @sorted_numbers = sort {$b <=> $a} @numbers; # or reverse sort {$a $b} @numbers
print “@sorted_numbers\n”;
[/perl]
The output is:
109 65 23 22 9 7 3 1
This was all about sorting array elements alphabetically or numerically. Now we will see how sorting works on hash elements.
Example 4: Sorting hashes by keys
You can use sort to order hashes. For example, if you had a hash as follows:
Suppose we want to display the members for each community sorted alphabetically or say by keys, then this code will do so:
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my %members = (
C => 1,
Java => 7,
Perl => 12,
Linux => 3,
Hacking => 8,
);
foreach my $language (sort keys %members) {
print $language . “: ” . $members{$language} . “\n”;
}
[/perl]
Output:
C: 1
Hacking: 8
Java: 7
Linux: 3
Perl: 12
If you want to sort the same hash by the values (i.e. the users beside each programming language), you could do the following:
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my %members = (
C => 1,
Java => 7,
Perl => 12,
Linux => 3,
Hacking => 8,
);
# Using <=> instead of cmp because of the numbers
foreach my $language (sort {$members{$a} <=> $members{$b}} keys %members){
print $language . “: ” . $members{$language} . “\n”;
}
[/perl]
Output:
C: 1
Linux: 3
Java: 7
Hacking: 8
Perl: 12
Example: 5 Sorting complex data structures
We can also use sort function to sort complex data structures. For example, suppose we have an array of hashes (anonymous hashes) like:
[perl]
my @aliens = (
{ name => ‘Jassi’, age => 28},
{ name => ‘Somnath’, age => 27},
{ name => ‘Ritesh’, age => 24},
{ name => ‘Santosh’, age => 29},
{ name => ‘Ranjan’, age => 26},
{ name => ‘Kaushik’, age => 25},
);
[/perl]
And we wish to display the data about the people by name, in alphabetical order, we could do the following:
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my @aliens = (
{ name => ‘Jassi’, age => 28},
{ name => ‘Somnath’, age => 27},
{ name => ‘Ritesh’, age => 24},
{ name => ‘Santosh’, age => 29},
{ name => ‘Ranjan’, age => 26},
{ name => ‘Kaushik’, age => 25},
);
foreach my $person (sort {$a->{name} cmp $b->{name}} @aliens) {
print $person->{name} . ” is ” . $person->{age} . “\n”;
}
[/perl]
The output is:
Jassi is 28
Kaushik is 25
Ranjan is 26
Ritesh is 24
Santosh is 29
Somnath is 27
Sorting the same hash by age and using a subroutine (inline function)
Rather than writing the code inline, you can also pass in a subroutine name. The subroutine needs to return an integer less than, equal to, or greater than 0. Do not modify the $a and $b variables as they are passed in by reference, and modifying them will probably confuse your sorting.
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my @aliens = (
{ name => ‘Jassi’, age => 28},
{ name => ‘Somnath’, age => 27},
{ name => ‘Ritesh’, age => 24},
{ name => ‘Santosh’, age => 29},
{ name => ‘Ranjan’, age => 26},
{ name => ‘Kaushik’, age => 25},
);
foreach my $person (sort agecomp @aliens) {
# it just replaced {$a->{age} <=> $b->{age}} by agecomp inline function
print $person->{name} . ” is ” . $person->{age} . ” years old\n”;
}
sub agecomp {
$a->{age} <=> $b->{age};
}
[/perl]
The output would be:
Ritesh is 24 years old
Kaushik is 25 years old
Ranjan is 26 years old
Somnath is 27 years old
Jassi is 28 years old
Santosh is 29 years old
To find out more on sort function, run the command on Linux box:
perldoc -f sort
Subscribe now to keep reading and get access to the full archive.