In this topic we're going to learn some basic things about Perl and about setting up an environment for Perl scripting.
-Installation on different platforms
-Getting different modules installed + CPAN
-Some hello-world's
-Perldoc
Perl is a high-level, interpreted language for everyday problems/projects. One of the best things about it is first, it's community (to answer you smart questions) and second, CPAN! (in my opinion)
Comprehensive Perl Archive Network, is where people share their made modules. Let's say, it has a module for mostly everything!
Just to say a bit more, Perl is a common lanuage for writting CGI scripts (web), too, however for now, we'll stick to general things.
PART 1 | InstallationNow it's time to install Perl interpreter on your system if you don't already have it. It's by default installed on most platforms like Linux and OS X, but not on Windows. So if you want to get it working, you can install ActiveState Perl, Strawbery or DWIM Perl as said in [http://perl.org/get.html]
Mac OS X has it by default, however if you don't have you can use ActiveState Perl, too.
[http://www.activestate.com/activeperl/downloads]
For Linux users there's also sources available you can compile & install. (by the time I'm writing this post, newest release is 5.22.0 available at
http://www.cpan.org/src/5.0/perl-5.22.0.tar.gz)
After you installed, you can test with the command perl -V, example:
root@ACPC:~# perl -v
This is perl 5, version 20, subversion 2 (v5.20.2) built for x86_64-linux-gnu-thread-multi
(with 42 registered patches, see perl -V for more detail)
Copyright 1987-2015, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
PART 2 | Modules[http://learn.perl.org/modules/]
What is a Perl module?
Perl modules external link are a set of related functions in a library file. They are specifically designed to be reusable by other modules or programs. There are 108,000 modules ready for you to use on the Comprehensive Perl Archive Network external link
Generally, modules make life easier.
Examples are Net::xxx modules like Net::SSH which lets you ssh to a server, or LWP::xxx that let you do easier/better HTTP requests (also WWW::Mechanize makes it even easier by including LWP and adding things to it) and etc etc.
To install modules you can:
- From source (either copy .pm files or do a "perl Makefile.PL"
- Use cpan
- Use ppm (Perl Package Manager by ActiveState Perl on Windows)
Here I'm going to write some simple examples about installing modules, however I recommend you to take a look at [http://www.perlmonks.org/?node_id=128077].
From sourceLet's choose a module for example, JSON, we first open up
https://metacpan.org/pod/JSON and click on "Download (83.33Kb)" to get the module source.
It would be something like "JSON-2.90.tar.gz", so we should extract it first, then we just enter 4 simple commands (shown below) and it'll be installed if there's no unmet dependencies. (dependencies are modules that are needed by the module we want to get it installed)
We do:
- perl Makefile.PL
- make
- make test
- make install
**make is part of build-essential package (on Debian based dirstros), to install:
# apt-get install build-essential
OR
# yum groupinstall "Development Tools"
**Attention: on Windows you do "nmake"
not "make" and you should get it installed (probably Cygwin or something).
root@localhost:~# tar xf JSON-2.90.tar.gz
root@localhost:~# #it's now extracted to JSON-2.90 directory
root@localhost:~# cd JSON-2.90
root@localhost:~/JSON-2.90# [b]perl Makefile.PL[/b]
Welcome to JSON (v.2.90)
=============================
--- snip ---
Checking if your kit is complete...
Looks good
Generating a Unix-style Makefile
Writing Makefile for JSON
Writing MYMETA.yml and MYMETA.json
root@localhost:~/JSON-2.90# [b]make[/b]
cp lib/JSON/backportPP/Boolean.pm blib/lib/JSON/backportPP/Boolean.pm
cp lib/JSON/backportPP.pm blib/lib/JSON/backportPP.pm
--- snip ---
root@localhost:~/JSON-2.90# [b]make test[/b]
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
--- snip ---
root@localhost:~/JSON-2.90# [b]make install[/b]
Manifying blib/man3/JSON::backportPP.3pm
--- snip ---
root@localhost:~/JSON-2.90# #done!
Using CPANIt's simple, you just enter "cpan MODULE_NAME", replacing MODULE_NAME with name of the module you want to install... .
root@localhost:~# cpan JSON
Reading '/root/.cpan/Metadata'
Database was generated on Mon, 21 Sep 2015 12:29:03 GMT
Running install for module 'JSON'
Fetching with LWP:
http://www.cpan.org/authors/id/M/MA/MAKAMAKA/JSON-2.90.tar.gz
--- snip ---
Using PPMTo use the GUI mode, enter "ppm" in cmd or run, or just run the exe file.
You'd probably see something like this:
http://docs.activestate.com/activeperl/5.8/images/ppm_gui.pngSee [http://docs.activestate.com/activeperl/5.8/faq/ActivePerl-faq2.html] for more.
PART 3 | Hello worlduse strict;
use warnings;
#always, try to use those ^
print "Hi, everyone!\n";
use strict;
use warnings;
use 5.10.0;
say "This is another way, without using \\n!";
use strict;
use warnings;
print "Enter something: ";
chomp(my $a = <STDIN>);
print "Enter another thing: ";
chomp(my $b = <STDIN>);
print "Concatenation! " . $a . " " . $b . "\n";
print "Better: $a $b\n";
use strict;
use warnings;
use LWP::Simple qw (getstore); #Maybe it's not installed on your system
getstore("http://google.com", "google.html");
PART 4 | Perldocperldoc is a command line tool showing the "man" pages for Perl modules, functions, variables and etc.
root@ACPC:~# perldoc
Usage: perldoc [-hVriDtumFXlT] [-n nroffer_program]
[-d output_filename] [-o output_format] [-M FormatterModule]
[-w formatter_option:option_value] [-L translation_code]
PageName|ModuleName|ProgramName
Examples:
perldoc -f PerlFunc
--- snip ---
For a example we want to see how the docs for WWW::Mechanize module,
perldoc WWW::Mechanize
or we want to know how "join" function works,
perldoc -f join
That's it, we talked about main installation, modules installation, cpan and perldoc, if you have any thing to add, I love to hear about.
---END <3