System Programming and Administration
Upcoming SlideShare
Loading in...5
×
 

System Programming and Administration

on

  • 750 views

This is the fourteenth (and last for now) set of slides from a Perl programming course that I held some years ago. ...

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

Statistics

Views

Total Views
750
Views on SlideShare
742
Embed Views
8

Actions

Likes
0
Downloads
5
Comments
0

1 Embed 8

http://www.linkedin.com 8

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

System Programming and Administration System Programming and Administration Presentation Transcript

  • Perl Programming Course System Programming and AdministrationKrassimir BerovI-can.eu
  • Contents1. Recommended Modules2. Running perl3. Command-line switches4. Filesystem analysis5. Mail processing6. Security notes
  • 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
  • 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
  • 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
  • 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
  • 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 • ...
  • 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.
  • 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.
  • 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|;}
  • 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.$/ }
  • 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
  • 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 • 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
  • 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.
  • 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();
  • 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");
  • 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
  • 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;
  • 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
  • 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
  • System Programming and Administration• Ressources • perldoc perlrun • perldoc perlsec • http://perltraining.com.au/notes/sysadmin.pdf • http://perltraining.com.au/notes/perlsec.pdf • etc...
  • System Programming andAdministrationQuestions?