I am far from a Perl mong. These are just a few notes. I recommend you read
a Perl tutorial to really understand how this works.
Configuration
Set a repository:sudo perl -MCPAN -e shell cpan> o conf urllist http://cpan.yahoo.com/On Ubuntu, several packages of Perl must be installed as Ubuntu packages. For example, the support for GD (pies, charts etc) is installed as package libgd-graph-perl.
If automatic installation via perl -MCPAN -e shell fails, it is possible to fix the problems and finish manually: go into the CPAN directory (e.g /root/.cpan). Go in the builds directory, then do:
perl Makefile.PL
Perl DBD installation on Solaris
To install the Perl package DBD::mysql, do the following in .cpan/builds:- remove -KPIC, -lCrun and Optimize in the Makefile.PL
- perl Makefile.PL
- make
- make test
- make install
Perl Script
#!/usr/bin/perl -w # use strict; use warnings;The first argument is stored in $ARGV[0]. The number of arguments is $#ARGV +1. If there are no arguments to the program, $#ARGV is -1.
Perl Programs
File management
Open a file for reading:my $line; open(FILE, "$filename") or die "Cant open $filename"; while ($line =To open a file for writing, use) { ... } close(FILE);
open(FILE, ">$filename");
Functions
sub mysub { # get the first parameter - beware does not work if this is a table ! my $file = shift; }
Mails
Simple email sending
To send a mail with Perl:use MIME::Lite; my $msg = MIME::Lite->new ( From => 'from@email.com', To => 'to@email.com', Subject => 'Your subject', Data => 'The content' ); MIME::Lite->send('smtp', 'mail.server.com', Timeout => 60); $msg->send();You may need to specify the login on the mail server: AuthUser, and the corresponding password: AuthPass.
To debug, set Debug => 1 (or more).
Sending emails with attachments
Adding an image:$msg->attach ( Type => 'image/gif', Path => $local_path_to_file, Filename => $filename, Disposition => 'attachment' ) or die "Error: $!\n";Adding a zip file: similar, but use
Type => 'application/zip',Source: Akadia
Sending mail with Gmail account
#!/usr/bin/perl -w use strict; use warnings; use Email::Send; use Email::Send::Gmail; use Email::Simple::Creator; my $email = Email::Simple->create( header => [ From => 'FROM@EMAIL.COM', To => 'TO@EMAIL.COM', Subject => 'YOUR SUBJECT', ], body => 'THE CONTENT OF THE MAIL.', ); my $sender = Email::Send->new( { mailer => 'Gmail', mailer_args => [ username => 'PUT YOUR GMAIL LOGIN', password => 'PUT YOUR GMAIL PWD', ] } ); eval { $sender->send($email) }; die "Error sending email: $@" if $@;
Objects in Perl
Declare and initialize the object:my $msg = { subject => "", from => "", date => "", size => 0 };Access a member field:
$msg->{subject}Increment a value: $msg->{size}++
print "Blah\n"; printf "%.2f" , $value;
Regular expressions
Perl makes an extensive use of regular expressions.Splitting a line
Use the split command, and specify the keyword and the variables to fill. The first one is filled with the leftmost part (before the keyword), the second one is filled with the part on the right of the keyword.($a, $b) = split(/keyword/, $line);assigns the left part to a and the right to b.
For example, the following line:
name=toto
($variable, $name) = split(/=/, $line);assigns name to $variable and toto to $name. Examples:
($msg->{size}, $key) = split(/;/, $basename); ($basename, $msg->{extension}) = split(/\./, $msg->{filename});
Matching a pattern
Tests if the $line begins with keyword (case sensitive):if ($line =~ /^Keyword/) { // begins with Keyword }Same but case insensitive:
if ($line =~ /^Keyword/i) {Tests if the line contains the keyword (not always at the beginning): remove the ^:
if ($line =~ /Keyword/) {Tests if the line contains 'Keyword' or 'Deyword':
if ($line =~ /[DK]eyword/) {Tests if the line contains a dot:
if ($line =~ /\./) {Some other characters such as / must be escaped. Tests if the line ends with a keyword (use the $ sign):
if ($line =~ /Keyword$/) {
Modify a value with a rule
Remove leading spaces:$msg =~ s/^\s+//;
Tables
Declare (use a @):my @table;Add to a table:
unshift(@table, $item);Erase an array:
$#array = -1;