open(SESAME, "filename"); # read from existing file open(SESAME, "<filename"); # (same thing, explicitly) open(SESAME, ">filename"); # create file and write to it open(SESAME, ">>filename"); # append to existing file open(SESAME, "| output-pipe-command"); # set up an output filter open(SESAME, "input-pipe-command |"); # set up an input filter |
print STDOUT "Enter a number: "; # ask for a number $number = <STDIN>; # input the number print STDOUT "The number is $number\n"; # print the number |
STDIN
, if none were
specified
read
does not automatically remove the newline from its input
line
chop($number = <STDIN>); # input number and remove newline |
Example | Name | Result |
---|---|---|
$a + $b | Addition | Sum of $a and $b |
$a * $b | Multiplication | Product of $a and $b |
$a % $b | Modulus | Remainder of $a divided by $b |
$a ** $b | Exponentiation | $a to the power of $b |
$a = 123; $b = 456; $c = 3; print $a + $b; # prints 579 print $a . $b; # prints 123456 $b = 3; print $a * $c; # prints 369 print $a x $c; # prints 123123123 |
Example | Name | Result |
---|---|---|
-e $a | Exists | True if file named in $a exists|
-r $a | Readable | True if file named in $a is readable|
-w $a | Writable | True if file named in $a is writable|
-d $a | Directory | True if file named in $a is a directory|
-f $a | File | True if file named in $a is a regular file|
-T $a | Text File | True if file named in $a is a text file
-e "/usr/bin/perl" or warn "Perl is improperly installed\n"; -f "/vmunix" and print "Congrats, we seem to be running BSD Unix\n"; |
0 # would become the string "0", so false 1 # would become the string "1", so true 10 - 10 # 10-10 is 0, would convert to string "0", so false 0.00 # becomes 0, would convert to string "0", so false "0" # the string "0", so false "" # a null string, so false "0.00" # the string "0.00", neither empty nor exactly "0", so true "0.00" + 0 # the number 0 (coerced by the +), so false. \$a # a reference to $a, so true, even if $a is false undef() # a function returning the undefined value, so false |
unless ($destination eq $home) { print "I'm not going home.\n"; } |
foreach $user (@users) { if ($user eq "root" or $user eq "lp") { next; } if ($user eq "special") { print "Found the special account.\n"; # do some processing last; } } |
while ($line = <FILE>) { if ($line =~ /http:/) { print $line; } } |
while (<FILE>) { print if /http:/; print if /ftp:/; print if /mailto:/; # What next? } |
/\d{7,11}/ |
/.*?:/ |
/\bFred\b/ |
next LINE if $line =~ /^#/; |
s/(\S+)\s+(\S+)/$2 $1/ |
s/^([^ ]+) +([^ ]+)/$2 $1/; # swap first two words /(\w+)\s*=\s*\1/; # match "foo = foo" /.{80,}/; # match line of at least 80 chars /^(\d+\.?\d*|\.\d+)$/; # match valid number |
if (/Time: (..):(..):(..)/) { $hours = $1; $minutes = $2; $seconds = $3; } |
if (/Version: *([0-9.]+)/) { $version = $1; } |
m/
PATTERN
/gimosx
1
) or false (""
). If no
string is specified via the =~
or !~
operator, the
$_ string is searched.
Modifier | Meaning |
---|---|
g |
Match globally, that is, find all occurrences. |
i |
Do case-insensitive pattern matching. |
m |
Treat string as multiple lines. (continued) |
o |
Only compile pattern once. |
s |
Treat string as single line. |
x |
Use extended regular expressions. |
/
"
open(TTY, '/dev/tty'); <TTY> =~ /^y/i and foo(); # do foo() if they want it |
if (/Version: *([0-9.]+)/) { $version = $1; } |
next if m#^/usr/spool/uucp#; |
$arg = shift; while (<>) { print if /$arg/o; # compile only once } |
if (($F1, $F2, $Etc) = ($foo =~ /^\s*(\S+)\s+(\S+)\s*(.*)/)) |
$foo
into the first two words and the
remainder of the line, and assigns those three fields to $F1
,
$F2
, and $Etc
. The conditional is true if any
variables were assigned, that is, if the pattern matched. Usually, though,
one would just write the equivalent split:
if (($F1, $F2, $Etc) = split(' ', $foo, 3)) |
s/
PATTERN
/
REPLACEMENT
/egimosx
PATTERN
, and if found,
replaces that match with the REPLACEMENT
text and returns
the number of substitutions made, which can be more than one with the
/g modifier. Otherwise it returns false (0).
Modifier | Meaning |
---|---|
e |
Evaluate the right side as an expression. |
g |
Replace globally, that is, all occurrences. |
i |
Do case-insensitive pattern matching. |
m |
Treat string as multiple lines. (continued) |
o |
Only compile pattern once. |
s |
Treat string as single line. |
x |
Use extended regular expressions. |
s/\bgreen\b/mauve/g; |
$path =~ s(/usr/bin)(/usr/local/bin); |
s/Login: $foo/Login: $bar/; |
($foo = $bar) =~ s/this/that/; |
$count = ($paragraph =~ s/Mister\b/Mr./g); |
$_ = 'abc123xyz'; s/\d+/$&*2/e; # yields 'abc246xyz' s/\d+/sprintf("%5d",$&)/e; # yields 'abc 246xyz' s/\w/$& x 2/eg; # yields 'aabbcc 224466xxyyzz' |
s/%(.)/$percent{$1}/g; # change percent escapes; no /e s/%(.)/$percent{$1} || $&/ge; # expr now, so /e s/^=(\w+)/&pod($1)/ge; # use function call # /e's can even nest; this will expand simple embedded variables in $_ s/(\$\w+)/$1/eeg; |
$program =~ s { /\* # Match the opening delimiter. .*? # Match a minimal number of characters. \*/ # Match the closing delimiter. } []gsx; |
s/^\s*(.*?)\s*$/$1/; |
s/([^ ]*) *([^ ]*)/$2 $1/; |
$
instead of \
in the last example.
@array = (1 + 2, 3 - 4, 5 * 6, 7 / 8); |
sort @guys, @gals, other(); |
print reverse sort map {lc} keys %hash; |