Perl Reference Card Page 2

ADVERTISEMENT

Syntax:
Extended Constructs
8 One-Liners
\
(?#text)
escape
comment
-0
(zero) specify the input record separator
.
any single char
(?imxs-imsx:...)
enable or disable option
-a
split data into an array named @F
^
start of line
(?=...), (?!...)
positive / negative look-ahead
-F
specify pattern for -a to use when splitting
$
end of line
(?<=..), (?<!..)
positive / negative look-behind
-i
edit files in place
*, *?
0 or more times (greedy / nongreedy)
(?>...)
prohibit backtracking
-n
run through all the @ARGV arguments as files, using <>
+, +?
1 or more times (greedy / nongreedy)
(?{ code })
embedded code
-p
same as -n, but will also print the contents of $_
?, ??
0 or 1 times (greedy / nongreedy)
(??{ code })
dynamic regex
\b, \B
word boundary ( \w - \W) / match except at w.b.
(?(cond)yes|no)
condition corresponding to captured parentheses
Interactive Mode: perl -de 42
(?(cond)yes)
condition corresponding to look-around
\A
string start (with /m)
Variables
\Z
string end (before \n)
Examples:
$&
entire matched string
\z
absolute string end
1. just lines 15 to 17, efficiently
$`
everything prior to matched string
\G
continue from previous m//g
perl -ne 'print if $. >= 15; exit if $. >= 17;'
$'
everything after matched string
[...]
character set
2. just lines NOT between line 10 and 20
$1, $2 ...
n-th captured expression
(...)
group, capture to $1, $2
perl -ne 'print unless 10 .. 20'
$+
last parenthesis pattern match
(?:...)
group without capturing
3. lines between START and END
$^N
most recently closed capt.
{n,m} , {n,m}?
at least n times, at most m times
perl -ne 'print if /^START$/ .. /^END$/'
$^R
result of last (?{...})
{n,}
, {n,}?
at least n times
4. in-place edit of *.c files changing all foo to bar
@-, @+
offsets of starts / ends of groups
{n}
, {n}?
exactly n times
perl -pi.bak -e 's/\bfoo\b/bar/g' *.c
|
or
5. delete first 10 lines
\1, \2
text from nth group ($1, ...)
perl -i.old -ne 'print unless 1 .. 10' foo.txt
Escape Sequences:
6. change all the isolated oldvar occurrences to newvar
7 Object-Oriented Perl and Modules
\a
alarm (beep)
\e
escape
perl -i.old -pe 's{\boldvar\b}{newvar}g' *.[chy]
\f
formfeed
\n
newline
7. printing each line in reverse order
Defining a new class:
\r
carriage return
\t
tab
perl -e 'print reverse <>' file1 file2 file3 ....
package Person;
\cx
control-x
\l
lowercase next char
8. find palindromes in the /usr/dict/words dictionary file
use strict;
\L
lowercase until \E
\U
uppercase until \E
perl -lne '$_ = lc $_; print if $_ eq reverse'
sub new { #constructor, any name is fine
\Q
diable metachars until \E
\E
end case modifications
/usr/dict/words
my $class = shift;
9. command-line that reverses all the bytes in a file
Character Classes:
my $self
= {};
perl -0777e 'print scalar reverse <>' f1 f2 f3
[amy]
'a', 'm', or 'y'
$self->{NAME} = undef; # field
10. word wrap between 50 and 72 chars
[f-j.-]
range f-j, dot, and dash
$self->{"_CENSUS"} = \$Census; # class data
perl -p000e 'tr/ \t\n\r/ /;
[^f-j]
everything except range f-j
++ ${ $self->{"_CENSUS"} };
s/(.{50,72})\s/$1\n/g;$_.="\n"x2'
\d, \D
digit [0-9] / non-digit
bless ($self, $class);
11. strip and remove double spaces
\w, \W
word char [a-zA-Z0-9_] / non-word
return $self;
perl -pe '$_ = " $_ "; tr/ \t/ /s; $_ =
char
}
substr($_,1,-1)'
\s, \S
whitepace [ \t\n\r\f] / non-space
sub name { #method
12. move '*.txt.out' to '*.out'
\C
match a byte
my $self = shift;
perl -e '($n = $_) =~ s/\.txt(\.out)$/$1/ and not
\pP, \PP
match p-named unicode / non-p-named-unicode
if (@_) { $self->{NAME} = shift }
-e $n and rename $_, $n for @ARGV' *
\p{...}, \P{...}
match long-named unicode / non-named-unicode
return $self->{NAME};
\X
match extended unicode
}
Posix:
sub DESTROY { #destructor
[:alnum]
alphanumeric
my $self = shift; -- ${$self->{"_CENSUS"} };}
[:alpha]
alphabetic
1;
# so the ‘require’ or ‘use’ succeeds
[:ascii:]
any ASCII char
[:blank:]
whitespace [ \t]
Using the class:
[:cntrl:]
control characters
use Person;
[:digit:]
digits
$him = Person->new();
[:graph:]
alphanum + punctuation
$him->name("Jason");
[:lower:]
lowercase chars
printf "There's someone named %s.\n", $him->name;
[:print:]
alphanum, punct, space
use Data::Dumper; print Dumper($him); # debug
[:punct:]
punctuation
[:space:]
whitespace [\s\ck]
[:upper:]
uppercase chars
[:word:]
alphanum + '_'
[:xdigit:]
hex digit
[:^digit:]
non-digit
Installing Modules: perl -MCPAN -e shell;

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 2