This is the fourteenth (and last for now) set of slides from a Perl programming course that I held some years ago.
I want to share it with everyone looking for intransitive Perl-knowledge.
A table of content for all presentations can be found at i-can.eu.
The source code for the examples and the presentations in ODP format are on https://github.com/kberov/PerlProgrammingCourse
3. Recommended Modules
• In no particular order
• Archive::Extract - A generic archive extracting
mechanism
• Carp - warn of errors (from perspective of caller)
• Config - access Perl configuration information
• Config::Extensions - hash lookup of which core
extensions were built.
• CPAN - query, download and build perl modules from
CPAN sites
• Cwd - get pathname of current working directory
• Data::Dumper - stringified perl data structures,
suitable for both printing and eval
4. Recommended Modules
• In no particular order
• Dumpvalue - provides screen dump of Perl data.
• Encode - character encodings
• ExtUtils::Install - install files from here to there
• ExtUtils::Installed - Inventory management of
installed modules
• ExtUtils::Liblist - determine libraries to use and how
to use them
• ExtUtils::MakeMaker - Create a module Makefile
• File::Basename - Parse file paths into directory,
filename and suffix.
• File::Compare - Compare files or filehandles
5. Recommended Modules
• In no particular order
• File::Copy - Copy files or filehandles
• File::Find - Traverse a directory tree.
• File::Path - create or remove directory trees
• File::Spec - portably perform operations on file
names
• File::stat - by-name interface to Perl's built-in stat()
functions
• File::Temp - return name and handle of a temporary
file safely
• FindBin - Locate directory of original perl script
6. Recommended Modules
• In no particular order
• Getopt::Long - Extended processing of command
line options
• Getopt::Std - Process single-character switches with
switch clustering
• Hash::Util - A selection of general-utility hash
subroutines
• IO::* - supply object methods for * handles
• IPC::* - processes...
• List::Util - A selection of general-utility list
subroutines
• Locale::* - ISO codes, localization etc.
• MIME::Base64 - Encoding and decoding of base64
strings
7. Recommended Modules
• In no particular order
• Net::* - network programming
• POSIX - Perl interface to IEEE Std 1003.1
• Scalar::Util - A selection of general-utility scalar
subroutines
• Storable - persistence for Perl data structures
• Test::More - yet another framework for writing test
scripts
• ...
8. Running perl
• Run a perl program by
• making it directly executable
• passing the name of the source file as an argument
on the command line
• Upon startup, Perl looks for your program in one of
the following places:
• Specified line by line via -e or -E switches on the
command line.
• Contained in the file specified by the first filename on
the command line.
• Passed in implicitly via standard input.
9. Running perl
• Run a perl program by
• making it directly executable
• passing the name of the source file as an argument
on the command line
• Upon startup, Perl looks for your program in one of
the following places:
• Specified line by line via -e or -E switches on the
command line.
• Contained in the file specified by the first filename on
the command line.
• Passed in implicitly via standard input.
11. Command-line switches
• Running code snippets
• This will tell you of syntax errors immediately,
but script execution will not start until you send
Perl an end-of-file character,
• On Unix systems - CTRL-D at the start of a line
• under Windows -CTRL-Z at the start of a line.
>perl
for(1..20){
print 'hi'.$/
}
12. Command-line switches
• Printing switch (-p)
• tells Perl to act as a stream editor
• will read input from STDIN, or from files
mentioned on the command line, and place each
line of input into $_.
• your program is executed, and the contents of $_
are printed.
• most commonly used with s///,
>perl -pe 's/perl/Python/gi' filesed.txt >filesed1.txt
14. Command-line switches
• In-place switch (-i)
• allows you to edit the file in place, overwriting
the original version
• a bug in your program can result in data-loss
• provide an argument to the switch: -i.old to
create a backup copy of the original file file.old
and then overwrite the original
> perl -i.old -pe 's/perl/python/gi' original.txt
15. Command-line switches
• Check switch (-c)
• check the program for syntactic errors and to
exit without executing the file
> perl -c script.pl
• Warnings switch (-w)
• runs your program with warnings turned on
• Include switch (-I)
• additional directories to be searched for
modules
• modifies Perl’s special @INC variable.
16. Filesystem analysis
• Directory separators
• use File::Spec to perform really OS
independent operations on directories
• use IO::Dir for directories manipulation
• use Cwd to get the current working dir.
use File::Spec::Functions qw(rel2abs);
#or
use File::Spec
#later...
$tmpdir = File::Spec->tmpdir();
$is_case_tolerant = File::Spec->case_tolerant();
17. Filesystem analysis
• Working with files
• use File::Copy to copy and move files
• use unlink() to delete files
• use File::Temp to safely create temporary
files
• use file-test operators to find information
about files
• see: perldoc -f -x
use File::Copy;
copy("file1","file2") or die "Copy failed: $!";
copy("Copy.pm",*STDOUT);
move("/dev1/fileA","/dev2/fileB");
18. Filesystem analysis
• Working with files
• use chmod() to change file permissions
• use umask() to change default file
permissions
• use chown() to change file ownership
• use File::Find to traverse directory trees
and recursively manipulate files in them
use Fcntl;
umask 0022;
sysopen(FILE, "runme", O_WRONLY|O_CREAT|O_EXCL, 0777);
#creates file with permissions 0755
19. Mail processing
• use Net::SMTP to send simple mail
• use MIME::Lite to send mail with attachments
• use Net::POP3 to retrieve mail
• use Mail::Audit to filter your messages
use Net::POP3;
# Constructors
$pop = Net::POP3->new('pop3host', Timeout => 60);
if ($pop->login($username, $password) > 0) {
my $msgnums = $pop->list;
# hashref of msgnum => size
foreach my $msgnum (keys %$msgnums) {
#do something
}
}
$pop->quit;
20. Security notes
• Taint checking
• rule: You may not use data derived from
outside your program to affect something
else outside your program at least, not by
accident.
• all data that comes from external sources
is tainted
#!/usr/bin/perl -wT # Taint mode is enabled
21. Security notes
• Tainted data will be considered
unsuitable for certain operations:
• Executing system commands
• Modifying files
• Modifying directories
• Modifying processes
• Invoking any shell
• Performing a match in a regular expression
using the (?{ ... }) construct
• Executing code using string eval
#!/usr/bin/perl -wT # Taint mode is enabled
22. System Programming and
Administration
• Ressources
• perldoc perlrun
• perldoc perlsec
• http://perltraining.com.au/notes/sysadmin.pdf
• http://perltraining.com.au/notes/perlsec.pdf
• etc...