Automating System Administration with Perl, Second Edition by David N. each MIME part and decide whether it is a PDF file based on the MIME content type. Welcome to Perl Training Australia's Perl for System Administration. The first and third to fifth of these are our course notes (PDF) which can be downloaded. This course leads the student from the basics of writing and running Perl scripts to more advanced safely and securely automate system administration tasks.
|Language:||English, Portuguese, German|
|ePub File Size:||26.89 MB|
|PDF File Size:||18.38 MB|
|Distribution:||Free* [*Register to download]|
SECOND EDITION. Automating System. Administration with Perl. David N. Blank- Edelman. O'REILLY®. Beijing • Cambridge • Farnham • Köln • Sebastopol. Perl for System Administration is aimed at all levels of administrators on the Unix, Windows NT, or MacOS platforms. Assuming only a little familiarity with Perl. NATE CAMPI is a UNIX and Linux system administrator by trade, cur- Perl scripts, but you should still get cfengine to execute them on your behalf.
Perl scripts are made up of a list of statements terminated by semicolons and declarations. The first line of a perl script uses the! When Perl detects the comment character within the program, it ignores the rest of the line. Many of the examples in this document use several comment characters to denote information about the lines of code following the comments.
The comment character is a good tool for helping you to document your own perl scripts within the code. So, first having a blank line and then a line with! On all systems, you run your Perl script by providing the script name as a parameter to the Perl interpreter. Symbolic references are only really needed for very advanced operations in Perl; for everything else the same job can be done faster and more cleanly using a hash.
As such, we will not mention symbolic references further in this course, except to say that you don t want to use them by mistake. Barewords are words in your programs with no identifying characteristics.
For each case of a bareword, Perl has to guess at run-time whether it s a string, or a call to a subroutine, and this can introduce bugs if Perl guesses differently to what you intended.
Since it s trivial to be clear on this distinction, you will never need to use barewords either. Warnings Warnings turns on helpful advice to let you know that Perl thinks you ve probably done something wrong.
These warnings aren t necessarily show-stoppers, but if you re getting them, it s worth spending some time wondering why. A few things that trigger warnings are: Trying to read from or write to an unopened filehandle, socket or device.
Perl for System Administration
Treating a string of non-numeric characters as if it were a number. Printing or performing comparisons with undefined values.
Assigning an odd number of elements to a hash collection of key-value pairs. Perl Basics Comments Comments are wonderful things which help future maintainers, including yourself in 6 months time, decipher your code. These should be liberally spread through your code.
To start a comment just add a. Your comment will then last until the end of line: This comment takes the whole line print "Hello World!
This allows the future maintainer of your code contact you, and tell you how grateful they are that you provided such good comments. It s also recommended you include the date at least a month and year when you wrote the code. Starting your program Each of your programs should start with:! Variable names must start with an alphabet character.
There are variables whose names do not conform to these rules, however they are Special variables.
Perl for System Administration. Jacinta Richardson Paul Fenwick
We ll cover them later. Perl has three basic variable types, and each is preceded by a punctuation character known as a sigil. Scalars Perl s fundamental type is the scalar. A scalar contains a single piece of information; such as a number, a character, a string, a filehandle, or a reference pointer. If you treat a scalar containing a number as a string, Perl will turn it into a string. If you treat a scalar containing a string as a number, Perl will try to turn it into a Perl Training Australia 9 20 Chapter 4.
Perl Basics number. Adding integers and floating point numbers results in a floating point result. If you want to coerce it back into an integer, that s possible too. If you assign a string to a variable which was previously a filehandle, Perl doesn t mind. However, limits may still exist due to environmental influences such as machine precision and memory availability.
There is no need to tell Perl how long your string will be. Quotes and interpolation Perl has two sets of quote that are used for delimiting strings. Double quotes " and single quotes. Double quotes interpolate while single quotes do not.
Act like a Lady, Think like a man
Interpolation allows us to add variables within a set of double quotes and have those variables be replaced with their contents. These are merely treated as pairs of characters within single quotes. Arrays can contain any number of scalars again within memory and other machine constraints , and there are no restrictions on what those scalars may contain.
The sigil for an array is an at-sign A mnemonic for this is looks like a for array or all. Perl Basics Array indexes start at 0. This is because we re getting a single thing from the array: a scalar.
More often than not, we actually want the length of the array, rather than the last index. Perl Training Australia 11 22 Chapter 4. Perl Basics Array length There is one inherently scalar piece of information for an array, and that is its length. Since Perl does it s best to do what I mean dwim , treating an array like a scalar will return its length. Hashes A hash is an unordered mapping of key-value pairs.
Every key and value must be a scalar. Hashes can contain any number of key-value pairs and, like arrays, there are no restrictions on the scalar contents, although the keys are always treated as strings.
To understand this mapping consider a telephone book. In the telephone book we have names keys which map to numbers values.
It is easy enough to find a telephone number given a name, but very time-consuming to find a name given a telephone number. Perl s hashes are the same. Likewise it doesn t make sense for a telephone book to have multiple entries for the exact same name and address details.
How would you know which number to call?
Index of /pdf/Gentoomen Library/Programming/Perl/
Thus, hash keys must be unique. There s no good mnemonic for this one. It behaves like an ordinary comma except it s bigger and therefore easy to see and it automatically quotes the value to its left. Values on the right hand side, still need to be quoted. This is because we re getting a single thing from the hash: a scalar. Hash size To find out how many pairs of keys and values we have, we have to use either the keys or values function.
These return all of the keys and values respectively. Taking the result of either function in a scalar context returns us the result we want.
Special Variables Perl has a number of special variables. It is usually pronounced as dollar underscore but is sometimes referred to simply as it.
Such as print. These may include filenames, switches, and other input. Perl Training Australia 13 24 Chapter 4.
The keys in this hash depend on your operating system. Changing values in this hash changes the environment for your program and any other processes it spawns. However, changes do not affect the parent process; in other words they are lost after your program has finished running. Conditionals and truth Perl s conditional structures should look pretty familiar to most programmers.
Books & Videos
However, before we start this section we should take a brief detour into what Perl views as true and false. In fact, it s easier to look at what Perl views as false, because this is a very short list. Perl sees the following four things as false: 1. The undefined value. The number zero: The string of the single digit zero: "0" or 0. The empty string: "" or. Everything else is true. The primary difference between them is one of precedence.
English-like operators have almost the lowest precedence possible and are always evaluated last. C-like operators have the same precedence as they do in C.
It is always possible to use parentheses to force the order of execution, and it is recommended that you do so if you feel any ambiguity exists. For more information read perldoc perlop. The elsif and else blocks are optional.
Multiple elsif blocks may appear after the if and before any else. For example the following two code snippets do the same thing. Perl Basics Trailing conditionals Perl provides trailing conditional statements. However only a single statement may appear on the left. Because the conditional appears on the right, trailing conditionals have the potential to reduce readability of your code. Common problems with variables Tips on avoiding problems with variables Controlling variable scoping Declaring variables with my Declaring variables with our Declaring variables with local Introducing the Variable Scoping Guidelines Variable Scoping Guidelines for complex programs Enable use strict Declare user-defined variables and define their scopes Pass data to subroutines using arguments Class ID.
System Administration Guide: Regular expressions Some quick examples: Introduction to Scripting 2 Comment is free, but facts are sacred. We operate throughout Australia and the Asia-Pacific region.
In the telephone book we have names keys which map to numbers values. A good alternative is to use the autodie module. Starting your program Each of your programs should start with: Compartmentalizing code with subroutines Contact Us: