Perl for System Administration
Total Page:16
File Type:pdf, Size:1020Kb
Perl for System Administration Jacinta Richardson Paul Fenwick Perl for System Administration by Jacinta Richardson and Paul Fenwick Copyright © 2006-2008 Jacinta Richardson ([email protected]) Copyright © 2006-2008 Paul Fenwick ([email protected]) Copyright © 2006-2008 Perl Training Australia (http://perltraining.com.au) Conventions used throughout this text are based upon the conventions used in the Netizen training manuals by Kirrily Robert, and found at http://sourceforge.net/projects/spork Distribution of this work is prohibited unless prior permission is obtained from the copyright holder. This training manual is maintained by Perl Training Australia, and can be found at http://www.perltraining.com.au/notes.html. This is revision 1.2 of Perl Training Australia’s "Perl for System Administrators" training manual. Table of Contents 1. About Perl Training Australia....................................................................................................... 1 Training....................................................................................................................................... 1 Consulting ................................................................................................................................... 1 Contact us.................................................................................................................................... 1 2. Introduction..................................................................................................................................... 3 Course outline ............................................................................................................................. 3 Assumed knowledge ................................................................................................................... 3 Module objectives ....................................................................................................................... 3 Platform and version details........................................................................................................ 3 The course notes.......................................................................................................................... 4 Other materials............................................................................................................................ 4 3. Why use Perl for System Administration? ................................................................................... 5 4. Perl Basics........................................................................................................................................ 7 In this chapter.............................................................................................................................. 7 Important basics .......................................................................................................................... 7 Help ................................................................................................................................... 7 Shebang line ...................................................................................................................... 7 Strictures and warnings ..................................................................................................... 7 Strict......................................................................................................................... 8 Warnings .................................................................................................................. 8 Comments.......................................................................................................................... 8 Starting your program........................................................................................................ 9 Variables...................................................................................................................................... 9 Scalars................................................................................................................................ 9 Quotes and interpolation........................................................................................10 Arrays ..............................................................................................................................10 Array lookups.........................................................................................................11 Changing array elements........................................................................................11 Adding array elements ...........................................................................................11 Counting backwards...............................................................................................11 Last index...............................................................................................................11 Array length ...........................................................................................................11 Interpolation...........................................................................................................12 Hashes..............................................................................................................................12 Hash lookups..........................................................................................................12 Changing hash values ............................................................................................12 Adding hash pairs ..................................................................................................13 Hash size ................................................................................................................13 Interpolation...........................................................................................................13 Special Variables .............................................................................................................13 $_............................................................................................................................13 @ARGV ................................................................................................................13 %ENV....................................................................................................................13 Conditionals and truth...............................................................................................................14 Comparison operators......................................................................................................14 Boolean operators............................................................................................................15 if-elsif-else.......................................................................................................................15 unless......................................................................................................................15 Perl Training Australia (http://perltraining.com.au/) iii Trailing conditionals ..............................................................................................15 Looping constructs....................................................................................................................16 while ................................................................................................................................16 foreach.............................................................................................................................16 Subroutines................................................................................................................................17 File I/O ......................................................................................................................................18 Reading............................................................................................................................19 Changing the input record separator......................................................................19 Writing.............................................................................................................................20 CPAN ........................................................................................................................................20 autodie.......................................................................................................................................21 Chapter summary ......................................................................................................................22 5. Regular expressions ......................................................................................................................23 In this chapter............................................................................................................................23 What are regular expressions?...................................................................................................23 Regular expression operators and functions..............................................................................23 m/PATTERN/ - the match operator .................................................................................23 s/PATTERN/REPLACEMENT/ - the substitution operator............................................24