Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Perl Programming                 Course            System Programming             and AdministrationKrassimir BerovI-can.eu
Contents1. Recommended Modules2. Running perl3. Command-line switches4. Filesystem analysis5. Mail processing6. Security n...
Recommended Modules• In no particular order  • Archive::Extract - A generic archive extracting    mechanism  • Carp - warn...
Recommended Modules• In no particular order  •   Dumpvalue - provides screen dump of Perl data.  •   Encode - character en...
Recommended Modules• In no particular order  • File::Copy - Copy files or filehandles  • File::Find - Traverse a directory...
Recommended Modules• In no particular order  • Getopt::Long - Extended processing of command    line options  • Getopt::St...
Recommended Modules• In no particular order  • Net::* - network programming  • POSIX - Perl interface to IEEE Std 1003.1  ...
Running perl• Run a perl program by  • making it directly executable  • passing the name of the source file as an argument...
Running perl• Run a perl program by  • making it directly executable  • passing the name of the source file as an argument...
Command-line switches• execute switch (-e) >perl   -e   print "Hello Worldn"  >perl   -e   "print qq{Hello Worldn}" # Wind...
Command-line switches• Running code snippets  • This will tell you of syntax errors immediately,    but script execution w...
Command-line switches• Printing switch (-p)  • tells Perl to act as a stream editor  • will read input from STDIN, or from...
Command-line switches• Module switch (-M)  • use modules in the one-liner > perl -MData::Dumper -e print Dumper(%ENV)
Command-line switches• In-place switch (-i)  • allows you to edit the file in place, overwriting    the original version  •...
Command-line switches• Check switch (-c)  • check the program for syntactic errors and to    exit without executing the fi...
Filesystem analysis• Directory separators  • use File::Spec to perform really OS    independent operations on directories ...
Filesystem analysis• Working with files  • use File::Copy to copy and move files  • use unlink() to delete files  •  use Fi...
Filesystem analysis• Working with files  • use chmod() to change file permissions  • use umask() to change default file    ...
Mail processing• use Net::SMTP to send simple mail• use MIME::Lite to send mail with attachments• use Net::POP3 to retriev...
Security notes• Taint checking  • rule: You may not use data derived from    outside your program to affect something    e...
Security notes• Tainted data will be considered  unsuitable for certain operations:  • Executing system commands  • Modify...
System Programming and             Administration• Ressources  • perldoc perlrun  • perldoc perlsec  • http://perltraining...
System Programming andAdministrationQuestions?
Upcoming SlideShare
Loading in …5
×

System Programming and Administration

1,322 views

Published on

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

Published in: Technology
  • Be the first to comment

  • Be the first to like this

System Programming and Administration

  1. 1. Perl Programming Course System Programming and AdministrationKrassimir BerovI-can.eu
  2. 2. Contents1. Recommended Modules2. Running perl3. Command-line switches4. Filesystem analysis5. Mail processing6. Security notes
  3. 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. 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. 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 Perls built-in stat() functions • File::Temp - return name and handle of a temporary file safely • FindBin - Locate directory of original perl script
  6. 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. 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. 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. 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.
  10. 10. Command-line switches• execute switch (-e) >perl -e print "Hello Worldn" >perl -e "print qq{Hello Worldn}" # Windows >perl -e for(1..20){print qq|hin|;}
  11. 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. 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
  13. 13. Command-line switches• Module switch (-M) • use modules in the one-liner > perl -MData::Dumper -e print Dumper(%ENV)
  14. 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. 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. 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. 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. 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. 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. 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. 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. 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...
  23. 23. System Programming andAdministrationQuestions?

×