Introducing UNIX and Linux |
PerlOverview |
String editingJust as the Perl command
For example, to replace $greeting =~ s/[Hh]ello/Howdy; Worked exampleWrite a Perl script which takes one argument, assumed to be the
name of a file which is itself a Perl program, and copies that file
to standard output with all comments removed. Assume that
open (FILE,$ARGV[0]);
while (<FILE>) {
$_ =~ s/#.*$//;
print "$_" ;
}
Worked exampleWrite a Perl script to read text from standard input, and send
to standard output the text with all leading/trailing whitespace
removed, and all sequences of tabs/spaces replaced with a single
space character.
while (<STDIN>) {
$_ =~ s/^[ \t]*//; # remove leading spaces
$_ =~ s/[ \t]*$//; # remove trailing spaces
$_ =~ s/[ \t][ \t]*/ /g; # squash internal whitespace
print "$_" ;
}
|
Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck