perl -w yourscript.pl.
e.g.
print "23abc" + 1;
#!/usr/bin/perl -w
e.g.
#!/usr/bin/perl -w print "23abc" + 1;
Argument "23abc" isn't numeric in addition (+) at hello.pl line 2. 24
-w option, no error message. Hard for debugging.
Reading: Chapter 6, p86-84
$line = <STDIN>; # read the next line
if($line eq "\n") {
print "That was just a blank line!\n";
} else {
print "That line of input was: $line";
}
The next program doesn't work as expect
# What's wrong witht the program
# try input Charles
$line = <STDIN>; # read the next line
if($line eq "Charles") {
print "Hi Charles!\n"; # never happen! even if you input Charles
} else {
print "You are not Charles!\n";
}
You have to remove the \n character!
$line = <STDIN>; # read the next line
chomp($line); # remove the last \n
if($line eq "Charles") {
print "Hi Charles!\n";
} else {
print "You are not Charles!\n";
}
or
chomp($line = <STDIN>); # read the next line and remove the last \n
if($line eq "Charles") {
print "Hi Charles!\n";
} else {
print "You are not Charles!\n";
}
Here is an example to go through a loop. Type Ctrl-C to stop the program.
while(defined($line = <STDIN>)) {
print "Echo:", $line;
}
Sample output
Hello Echo:Hello Charles Echo:Charles
Another shortcut to do it
while(<STDIN>) {
print "Echo:", $_
}
while(<STDIN>) {
chomp($_);
if($_ eq "Charles") {
print "Hi Charles!\n";
}
}
while(<STDIN>) {
chomp;
if($_ eq "Charles") {
print "Hi Charles!\n";
}
}
<STDIN> by <>
$i = 0;
$name = "Amy";
while(defined($line=<>)) {
chomp($line);
$i++;
if($line eq $name) {
print "line ", $i, " is ", $name, ".\n";
}
}
amy.plperl -w amy.pl (or amy.pl on Unix):
read the input from stardard input.perl -w amy.pl file1.txt (or amy.pl file1.txt on Unix):
read the input from file1.txtperl -w amy.pl < file1.txt (or amy.pl < file1.txt on Unix):
read the input from file1.txtperl -w amy.pl file1.txt file2.txt (or amy.pl file1.txt file2.txt on Unix):
read the input from file1.txt and file2.txt. You can read more then one filesperl -w amy.pl file1.txt > out.txt (or amy.pl file1.txt > out.tx on Unix):
read the input from file1.txt output the result to out.txt.Amy Betty Amy Dan Jenny Amy Dan Andyfile2.txt
Charles Eve Amy Dan Jenny Amy Dan AndyOutput of
perl -w amy.pl file1.txt (or other similar command)
line 1 is Amy. line 3 is Amy. line 6 is Amy.Output of
perl -w amy.pl file1.txt file2.txt (or other similar command)
line 1 is Amy. line 3 is Amy. line 6 is Amy. line 11 is Amy. line 14 is Amy.The program can be rewitten
$i = 0;
$name = "Amy";
while(<>) {
chomp($_);
$i++;
if($_ eq $name) {
print "line ", $i, " is ", $name, ".\n";
}
}
Reading: chapter 15, p208-211
$test = "C++ is great. Perl is the best."; # the length of the string print length($test), "\n"; #31 # where is "is"? print index($test, "is"), "\n"; #4 # where is "is" after the 10th position print index($test, "is", 10), "\n"; #19 # where is "abc" (not exists, return -1), print index($test, "abc"), "\n"; #-1 # where is "is" (start from the right); print rindex($test, "is"), "\n"; #19 # where is "is", before the 10th position print rindex($test, "is", 10), "\n"; #4
$test = "C++ is great. Perl is the best."; # substring after the 14th character # Perl is the best. print substr($test, 14), "\n"; # substring starts with 13th character, with 4 characters # Perl print substr($test, 14, 4), "\n"; # counts from the right, start with the 5th character # (the counting starts with 1, not 0) # then counts 4 letters (from left to right) # best print substr($test, -5, 4), "\n"; # replace C++ by Java # notice that the length of substitute is not even same as # the original substring substr($test, 0, 3) = "Java"; print $test, "\n";