Introduction to Perl II

perl -w

Standard Input and Output

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";
    }
}

Get Inputs from file

$i = 0;
$name = "Amy";
while(defined($line=<>)) {
    chomp($line);
    $i++;
    if($line eq $name) {
        print "line ", $i, " is ", $name, ".\n";
    }
}
Example
file1.txt
Amy
Betty
Amy
Dan
Jenny
Amy
Dan
Andy
file2.txt
Charles
Eve
Amy
Dan
Jenny
Amy
Dan
Andy
Output 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";
    }
}

String

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";