Johan Vromans, Squirrel Consultancy - Programming Perl 5.pdf

(215 KB) Pobierz
Quick Reference Guide
Quick Reference Guide
Perl
5
004
Johan Vromans
Squirrel Consultancy
Programming
15447454.025.png 15447454.026.png 15447454.027.png 15447454.028.png 15447454.001.png 15447454.002.png 15447454.003.png 15447454.004.png 15447454.005.png 15447454.006.png 15447454.007.png 15447454.008.png 15447454.009.png 15447454.010.png 15447454.011.png 15447454.012.png 15447454.013.png 15447454.014.png 15447454.015.png 15447454.016.png 15447454.017.png
Contents
1. Command line options ::::::::::::::::::
3
2. Syntax ::::::::::::::::::::::::::
4
3. Variables :::::::::::::::::::::::::
4
4. Literals ::::::::::::::::::::::::::
5
5. Operators and precedence
6
6. Statements ::::::::::::::::::::::::
7
7. Subroutines, packages and modules
7
8. Pragmatic modules ::::::::::::::::::::
9
10
10. Arithmetic functions ::::::::::::::::::: 10
11. Conversion functions ::::::::::::::::::: 11
12. Structure conversion ::::::::::::::::::: 11
13. String functions
9. Object oriented programming
12
14. Array and hash functions ::::::::::::::::: 12
15. Regular expressions
14
17. File test operators
16
18. File operations :::::::::::::::::::::: 16
19. Input / Output
17
19
21. Directory reading routines :::::::::::::::: 19
22. System interaction :::::::::::::::::::: 19
23. Networking
21
24. SystemV IPC ::::::::::::::::::::::: 21
25. Miscellaneous :::::::::::::::::::::: 22
26. Information from system files
23
27. Special variables ::::::::::::::::::::: 24
28. Special arrays
25
26
30. Environment variables :::::::::::::::::: 30
31. The perl debugger :::::::::::::::::::: 30
Conventions
fixed denotes text that you enter literally.
THIS
means variable text, i.e. things you must fill in.
THIS y
means that THIS will default to $_ if omitted.
word
is a keyword, i.e. a word with a special meaning.
RET
denotes pressing a keyboard key.
[...]
denotesan optionalpart.
2
20. Formats
29. Standard modules
15447454.018.png 15447454.019.png
1.Commandlineoptions
-a turns on autosplit mode when used with -n or -p . Splits to @F .
-c checks syntax but does not execute. It does run BEGIN and END blocks.
-d [ : DEBUGGER ]
runs the script under the debugger. Use ‘ -de 0 ’ to start the debugger
without a script.
-D NUMBER
sets debugging flags.
-e COMMANDLINE
may be used to enter a single line of script. Multiple -e commands may
be given to build up a multi-line script.
-F REGEXP
specifies a regular expression to split on if -a is in effect.
-h prints the Perl usage summary. Does not execute.
-i EXT
files processed by the <> construct are to be edited in-place.
-I DIR with -P : tells the C preprocessor where to look for include files. The
directory is prepended to @INC .
-l [ OCTNUM ]
enables automatic line ending processing, e.g. -l013 .
-m MODULE
imports the MODULE before executing the script. MODULE may be
followedbya‘ = ’ and a comma-separated list of items.
-M MODULE
Same as -m , but with more trickery.
-n assumes an input loop around the script. Lines are not printed.
-p assumes an input loop around the script. Lines are printed.
-P runs the C preprocessor on the script before compilation by Perl.
-s interprets ‘ -xxx ’ on the command line as a switch and sets the
corresponding variable $xxx in the script.
-S uses the PATH environment variable to search for the script.
-T turns on taint checking.
-u dumps core after compiling the script. To be used with the undump
program (where available).
-U allows Perl to perform unsafe operations.
-v prints the version and patchlevel of your Perl executable.
-V [ : VAR ]
prints Perl configuration information.
-w prints warnings about possible spelling errors and other error-prone
constructs in the script.
-x [ DIR ]
extracts Perl program from the input stream. If DIR is specified, switches
to this directory before running the program.
-0 [ VAL ]
(that’s the number zero) designates an initial value for the record
separator $/ . See also -l .
Command line options may be specified on the ‘ #! ’ line of the perl script, except
for -M , -m and -T .
3
15447454.020.png 15447454.021.png
2.Syntax
Perl is a free-format programming language. This means that in general it does not
matter how the Perl program is written with regard to indentation and lines.
An exception to this rule is when the Perl compiler encounters a ‘sharp’ symbol
( # ) in the input: it then discards this symbol and everything it follows up to the end
of the current input line. This can be used to put comments in Perl programs. Real
programmers put lots of useful comments in their programs.
There are places where whitespace does matter: within literal texts, patterns and
formats.
If the Perl compiler encounters the special token __END__ it discards this symbol
and stops reading input. Anything following this token is ignored by the Perl
compiler, but can be read by the program when it is run.
3.Variables
$var a simple scalar variable.
$var[28] 29th element of array @var .
$p = \@var now $p is a reference to array @var .
$$p[28] 29th element of array referenced by $p .Also: $p->[28] .
$var[-1] last element of array @var .
$var[$i][$j] $j -th element of $i -th element of array @var .
$var{’Feb’} a value from ‘hash’ (associative array) %var .
$p = \%var now $p is a reference to hash %var .
$$p{’Feb’} a value from hash referenced by $p .Also: $p->{’Feb’} .
$#var last index of array @var .
@var the entire array;
in a scalar context, the number of elements in the array.
@var[3,4,5] a slice of array @var .
@var{’a’,’b’} a slice of %var ;sameas ($var{’a’},$var{’b’}) .
%var the entire hash;
in a scalar context, true if the hash has elements.
$var{’a’,1,...} emulates a multi-dimensional array.
(’a’..’z’)[4,7,9] a slice of an array literal.
PKG :: VAR a variable from a package, e.g. $pkg::var , @pkg::ary .
\ THINGIE reference to a thingie, e.g. \$var , \%hash .
* NAME refers to all thingies represented by NAME .
*n1 = *n2 ’ makes n1 an alias for n2 .
*n1 = \$n2 ’ makes $n1 an alias for $n2 .
You can always use a { BLOCK } returning the right type of reference instead of
the variable identifier, e.g. ${ ... } , &{ ... } . $$p is just a shorthand for ${$p} .
4
15447454.022.png
4.Literals
Numeric: 123 1_234 123.4 5E-10 0xff (hex) 0377 (octal).
String: ’abc’ literal string, no variable interpolation nor escape characters, except
\’ and \\ .Also: q/abc/ . Almost any pair of delimiters can be used
instead of / ... / .
"abc" Variables are interpolated and escape sequences are processed.
Also: qq/abc/ .
Escape sequences: \t (Tab), \n (Newline), \r (Return), \f
(Formfeed), \b (Backspace), \a (Alarm), \e (Escape), \033 (octal),
\x1b (hex), \c[ (control).
\l and \u lowcase/upcase the following character;
\L and \U lowcase/upcase until a \E is encountered.
\Q quote regexp characters until a \E is encountered.
COMMAND evaluates to the output of the COMMAND .
Also: qx/ COMMAND / .
Boolean: Perl has no boolean data type. Anything that evaluates to the null string,
the number zero or the string "0" is considered false , everything else is
true (including strings like "00" !).
Array: (1,2,3) a three member array. () is an empty array.
(1..4) is the same as (1,2,3,4) . Likewise (’abc’..’ade’) .
qw/foo bar ... / is the same as (’foo’,’bar’, ... ) .
Array reference: [1,2,3] .
Hash (associative array): ( KEY1 , VAL1 , KEY2 , VAL2 , ... ) .
Also: ( KEY1 => VAL1 , KEY2 => VAL2 , ... ) .
Hash reference: { KEY1 , VAL1 , KEY2 , VAL2 , ... } .
Code reference: sub { STATEMENTS }
Filehandles: STDIN , STDOUT , STDERR , ARGV , DATA .
User-specified: HANDLE , $ VAR .
Globs: < PATTERN > evaluates to all filenames according to the pattern.
Use ‘ <${ VAR }> ’or‘ glob $ VAR ’ to glob from a variable.
Here-Is: << IDENTIFIER Shell-style ‘here document’.
Special tokens:
__FILE__ :filename; __PACKAGE_ _ : package; __LINE_ _ : line
number.
__END__ : end of program; remaining lines can be read using filehandle
< DATA > .
5
15447454.023.png 15447454.024.png
Zgłoś jeśli naruszono regulamin