• Keine Ergebnisse gefunden

Practical Perl Programming

N/A
N/A
Protected

Academic year: 2023

Aktie "Practical Perl Programming"

Copied!
104
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

Prerequisites

New Versions of This Document

Questions

Grading Policies

  • Blocks
  • Problems
  • Points
  • Deadlines
  • Revisions
  • Platforms
  • Delivery
  • Format
  • Collective Work

18 of the possible marks for each problem will be deducted from the grade for each day past the deadline that a late assignment is received. Each student who contributes to a solution jointly submitted by N >1 students will receive 2N3 of the total marks awarded to that solution.

Acquiring Perl

Students who choose to collaborate may do so, provided all students contributing to the solution of a problem submit exactly one collective solution to that problem.

Perl Resources

Shameless Plugs

  • Copying
  • FAQs and Factoids
  • Uses of Perl
  • Running perl
  • Compiling vs. Interpreting
  • Program Elements

Even writing a Perl program itself: Perl's flexibility gives programmers many ways to accomplish a given task. To translate source code (ie, of a program) into a binary machine-readable format ("object code") in preparation for the execution or further linking of that code.

Scalars: hello-name

Scalar Variables

Program Elements

One of the three built-in files7, STDIN usually receives data from the user's keyboard. The VARon variable on the left side of "=" is assigned the result of evaluating the expressionEXPR on the right side; here, the scalar.

Lists and Arrays: hello-folks

  • Lists and Arrays
  • Basic Array Operations
  • Program Elements
  • Black Magic

Returns the $nth element of the array@array, where $array[0]is the first element in@array. Returns a string constructed by concatenating the string representations of the elements of LIST, separated by the string SEP.

Hashes: hello-dialect

Hashes and Associative Arrays

The line input<> operator without a file handle reads from all files specified on the command line (as written in array@ARGV), or from standard input if no file is specified. In a list context, the line input operator (with or without a specified file handle) returns a list of all remaining lines in the file(s) handles it reads from.

Basic Hash Operations

Program Elements

Filehandles: hello-file.perl

Streams and Filehandles

Intuitively, you can read data from an input stream and you can write it to an output stream. "Pure" Perl file handles are always literal - see the IO::File(3pm) module manual for one solution.

Basic Filehandle Operations

Program Elements

Subroutines: hello-sub

  • Subroutines and Other Animals
  • Program Elements
  • Comments and Whitespace
  • Terms and Values
  • Expressions
  • Statements
  • Blocks
  • Declarations
  • Control Structures

Comments in Perl start with a "#" (hash) character and continue to the end of the line. A statement is either an expression whose value we choose to ignore (using the semicolon “;”) operator, or a directive to the Perl compiler known as a pragma.

Perl Datatypes

  • Scalars
  • Context
  • Lists and Arrays
  • Hashes
  • Subs and Code
  • Typeglobs and Filehandles
  • Regular Expression Patterns
  • References

An undefined "undef" value evaluates to zero when used as a number, and evaluates to an empty string when used as a string - in either case, expect "perl -w" to complain. See the perlop(1) man page and the pack() and unpack() entries in the perlfunc(1) man page for details. Enclosing an expression with parentheses is a good way to get perl to evaluate it in the context of a list - but it doesn't always work.

You may hear and/or read about animals such as "void context", "don't care context", "boolean context", "interpolative context" and/or "object context" - these can generally be thought of as variations on the theme of. Lists of key-value pairs serve the same purpose – but see the description of the “=>” operator below.

Perl Control Structures

  • Conditionals
  • Loops
  • Jumps
  • Subroutines
  • Declarators and Scope
  • Errors and Warnings
  • Dynamic Evaluation
  • External Code

Like the "break" statement in C, Perl's "own" command causes an immediate break in the innermost enclosing loop (first form) or the loop labeled with LABEL (second form). Like the "continue" statement in C, Perl's "next" command causes Perl to skip the rest of the current iteration and start the next iteration. 35The value of a loop statement such as "while" is usually the value of the last evaluated test condition, which by definition is a false value.

The return value of the evaluation is the value of the last statement evaluated in the BLOCK. Otherwise, if the file is read and compiled successfully, then completion returns the value of the last expression evaluated in the file.

Perl I/O

  • Filehandles
  • Files
  • Pipes
  • IO::File
  • Sockets

In a special case, you can omit the HANDLE argument on the line feed operator, which will read input from the files named by your script's command line arguments42, or from STDIN if no arguments are specified. You can also use the built-in line entry operator on an IO::File object. To use INET sockets, one machine (the “server”) must listen() on a particular port, and the other machine (the “client”) must connect() to that port on the server – usually the client must “know” the server's IP address to make this work.

You can do everything in the rest of this section without this module, but it's much uglier without it. Like IO::File, the IO::Socket class inherits from the abstract class IO::Handle, so you can use the standard methods such as print(), read(),close(), as well as the line input operator on connected ( client).

Figure 2: Input redirection from file
Figure 2: Input redirection from file

Perl Regular Expressions

  • Friends and Relations
  • Common Uses
  • Single-Character Patterns
  • Multi-Character Patterns
  • Grouping Patterns
  • Matching Miscellany

This section describes the main elements of a regular expression pattern for matching individual characters in an input string. This section describes the main elements of the regular expression pattern for matching multiple characters in an input string. "?" The (ask) pattern operator matches zero or one occurrences in the input string of the pattern element immediately preceding it.

Multipliers: * (Transitive and Reflexive Closure) The pattern operator "*" (asterisk) matches zero or more occurrences in the input string of the pattern element immediately preceding it. The "+" (plus) pattern operator matches one or more occurrences in the input string of the pattern element immediately preceding it.

Perl References

  • What is a Reference?
  • Why References?
  • Symbolic References
  • Hard References
  • Reference Counts and Memory Management
  • Stringification

In addition to being cumbersome, symbolic references can also be dangerous—the named variable underlying the symbolic reference may go out of scope before the reference is used, or its value may be "dirtyed" by another variable with the same name. You can think of funny character prefixes as evaluated from right to left: thus "$$array_ref[0]" is the first element of the array referred to by the hard reference "$array_ref", not the scalar referred to up to the first element ( normal) arrays “@array_ref”. This is why closures56 work: a closure (an anonymous subroutine) can contain a reference to a lexical variable that has since gone out of scope, but its thing still exists because of the reference to it encoded in the closure.

This is because each child node (labeled 'NP' and 'VP') contains a reference to the root node as the value of the key 'mother' for the child node, leaving the reference count for the root node. 2, even after the reference held by the variable$tree has gone out of scope. Since the value of the 'dtrs' key for the root node contains references to the daughters, the daughters themselves will not be deallocated either.

Figure 5: Symbol Tables and Hard References
Figure 5: Symbol Tables and Hard References

Perl Modules etc

  • What’s it All About?
  • Packages
  • Modules
  • Objects

The names of nested packages are of the form OUTER::INNER, where OUTER is the (possibly nested) "parent" package and INNER is the identifier of the nested package. The actual contents of a package's symbol table can be accessed via the hash %NAME:: for a package named NAME, which contains typeglob values ​​keyed by identifiers. If a package subroutine that still exists is called, but the package defines a separate subroutine called AUTOLOAD, then the package AUTOLOAD will be called with the arguments passed to the subroutine "missing" and the package global variable $AUTOLOAD will contain the full name of specified of the "missing" subroutine, as a string.

The fully qualified name of the current package is contained in the special keyword "PACKAGE" (two leading and two trailing underscores). Built-in Perl functions (such as print, chdir, etc.) can be overridden by a subroutine within a given package, and the overrides can even be propagated out of the package via a symbol-sharing mechanism.

Efficiency

  • Time Efficiency
  • Space Efficiency
  • Programmer Efficiency
  • Maintainer Efficiency
  • Porter Efficiency
  • User Efficiency

I personally tend to rate space efficiency as very important, but usually prefer time efficiency when the two conflict. Don't store data (like input) in an array when a loop over your source data (your input stream(s)) will suffice. Personally, I tend to rate programmer efficiency lower than just about every other flavor, except when I'm just trying to find a piece of code that does what I need it to do in a hurry - ie.

Use the modules from CPAN - the best program is one you don't have to write yourself. I personally tend to rate user efficiency quite highly; then again, I myself tend to be the primary user of my own programs.

Coding With Style

Indentation

Blank Lines

Comments

I like to use the "=" and "-" characters to make long (60–70 characters) horizontal lines for larger groups, with shorter lines for smaller conceptual groups:.

When Things Go Wrong

Grokking the Diagnostics

Common Warnings

Perl Errors

The Perl Debugger

Assignment to an array variable – here the variable @folks gets as its value the list consisting of the strings "moocow" and "Bryan", in that order. A built-in Perl function that returns the elements of its argument (a list, here the contents of the array@people) sorted in alphabetical order.

The value of an assignment is the assigned value – here the line read by . If there is still data to be read from STDIN since the terminating new-line is included in the value returned by , the expression.

References work just like C "pointers" to one of Perl's basic data types - SCALAR, ARRAY, HASH, or CODE. LABEL: for each VAR (LIST) BLOCK continue BLOCK Executes BLOCK once for each element of list valueLIST with that value associated with the VAR scalar loop variable. Declares a subroutine (read "function", "procedure" or "method") named NAME as a shorthand for the command-blockBLOCK (also known as the . "body" of the subroutine).

User-defined subroutines can specify how to calculate their values ​​by using the "return" statement, which causes the currently executing subroutine to immediately terminate and evaluate to EXPIf the given value is given, otherwise toundef. If your subroutine does not explicitly return any value, it will return the value (if any) of the last expression in the BLOCK body that it evaluates. The pattern operator “{m, n}” matches at least m but no more than n occurrences in the input string of the immediately preceding pattern element.

Two regular expression pattern elements separated by the alternation pattern operator "|" (vertical bar) matches any string that matches one or both of the two pattern elements:. User requires $VERSION to ensure the porter is running a sufficiently recent version of the Perl interpreter.

Figure 1: Program call without redirection
Figure 1: Program call without redirection

Abbildung

Figure 1: Program call without redirection
Figure 2: Input redirection from file
Figure 4: A simple pipeline
Figure 5: Symbol Tables and Hard References

Referenzen

ÄHNLICHE DOKUMENTE

On top of existing academic resources, such as the Corpus of Contemporary American English (COCA) academic Word List, the New Academic Word List, and the Academic Collocation List,