open(FILE, "/home/4user3/~bob/orders.log"); # read open(FILE, "</home/4user3/~bob/orders.log"); # read (< is optional) open(FILE, ">/home/4user3/~bob/orders.log"); # write open(FILE, ">>/home/4user3/~bob/orders.log"); # append
close(FILE);
open(FILE, ">>/home/4user3/~bob/orders.log")
|| &errorMessage("cannot open file orders.log");
sub errorMessage {
my $message = shift @_;
print "Content-Type: text/html\n\n";
print $message;
exit; # exit from the script
}
open(FILE, ">/home/4user3/~bob/orders.log"); print FILE "Order $oid was shipped $date.\n"; # writes to FILE print "Your order was shipped today.\n"; # writes to STDOUT
open(FILE, ">>/home/4user3/~bob/orders.log"); flock(FILE, 2); # obtains lock print FILE"Order 123 shipped 01/18/2000."; flock(FILE, 8); # releases lock
open(FILE, "/home/4user3/~bob/orders.log");
while (<FILE>) {
# process the line
# each line will be stored in the variable $_
}
close(FILE);
open(FILE, "/home/4user3/~bob/orders.log"); @messages = <FILE>; close(FILE);
$oldName = "orders.log"; $newName = "messages.log"; rename($oldName, $newName);
$fileName = "orders.log"; unlink($fileName);
opendir(DIR, "/home/4user3/bob"); opendir(CURRENT_DIR, "."); opendir(PARENT_DIR, "..");
close(DIR);
opendir(DIR, "/home/4user3/bob");
$file = readdir(MYDIR);
while ($file) {
# processing for that directory
# read next file or directory name
$file = readdir(MYDIR);
}
close(DIR);
opendir(DIR, "/home/4user3/bob"); @files = readdir(DIR); # names of files and directories close(DIR);
chdir("../data");
mkdir("catalog", 0777);
rmdir("tmp");
open(MAIL, "| /usr/bin/sendmail -t"); print MAIL "To: $to\n"; print MAIL "From: $from\n"; print MAIL "Subject: $subject\n"; print MAIL "$contents\n"; close(MAIL);
$text =~ m/pattern/[modifiers]
$text =~ m/pattern/i # ignores case $text =~ m/pattern/m # treats text as multiple lines (with \n) $text =~ m/pattern/s # treats text as single lines $text =~ m/pattern/g # matches all possible occurrences (iterator) $text =~ m/pattern/o # interpolates only once (optimization)
$text =~ /pattern/
$browser = $ENV{'HTTP_USER_AGENT'};
if ($browser =~ /MSIE/) {
$isMicrosoft = 1;
} elsif ($browser =~ /Mozilla/) {
$isNavigator = 1;
}
$text =~ s/pattern/replacement/[i][m][s][g][e]
$text =~ s/pattern/replacement/i # ignores case $text =~ s/pattern/replacement/g # replaces all occurrences $text =~ s/pattern/replacement/e # evaluates replacement
$feedback = $q->param('feedback'); # CGI.pm
$feedback =~ s/<IMG[^>]*>//ig;
split(/pattern/, $text);
BC:6.25 ON:7.00 QC:9.00 NB:5.00
sub salesTax {
my $p = shift @_;
open(FILE, "/home/shop/salestax");
while (<FILE>) {
my ($province, $tax) = split(/:/, $_);
if ($province eq $p) { return $tax; }
}
return 0.0;
}
^\d{3}-\d{4}$ # phone numbers (e.g. 520-2600)
a # matches the letter a \$ # matches the special character $ \n # matches newline \t # matches tab . # matches any character except \n \. # matches a period
[abc] # matches a, b, or c [a-z] # matches a range of characters [a-zA-Z] # matches all lowercase and uppercase [a\-z] # matches just a, -, or z
[^0-9\-] # matches everything except 0-9 and -
\d # matches any digit (same as [0-9]) \D # matches any non-digit \w # matches any letter, digit or underscore \W # matches any character not in \w \s # matches any space, \t, \n, \r or \f \S # matches any character not in \s
abc # matches abc ab?c # matches ac or abc (ab)+ # matches ab, abab, etc. a(b|c)d # matches abd or acd
ab?c # matches ac or abc
ab*c # matches ac, abc, abbc, etc.
(ab)+ # matches ab, abab, etc.
ab{2,4} # matches abb, abbb, and abbbb
(ab){3} # matches ababab
a{3,} # matches aaa, aaaa, etc.
ab|cd # matches ab or cd a(b|c)d # matches abd or acd
^http:
\.gif$
\b[A-Z][a-z]*\b # matches any capitalized word
$header = "Subject: Your Order"; $header =~ /$(\w+):\s+(\w*)/; $tag = $1; # Subject $value = $2; # Your Order
$header = "Subject: Your Order"; ($tag, $value) = $header =~ /$(\w+):\s+(\w*)/;
$_ = "Subject: Your Order"; ($tag, $value) = /$(\w+):\s+(\w*)/;
sub verifyPassword {
my ($user, $password) = @_;
open(PWD, "/home/alice/password");
while (<PWD>) {
if (/^$user:$password$/) { return 1; }
}
return 0;
}
while ($text =~ /but/) {
buts$++;
}
print "Your text contained $buts buts.";
$_ = "Greetings, planet Earth!"; /G.*t/ # matches Greetings, planet Eart $_ = "This 'test' isn't successful"; /'.*'/ # matches 'test' isn'
$_ = "Greetings, planet Earth!"; /G.*?t/ # matches Greet
$_ = "This 'test' isn't successful"; /'[^']*'/ # matches 'test' only