Using substitutions to clean up input data.
#!/usr/bin/perl -w
foreach $i (0 .. $#ARGV) {
open(INPUT, $ARGV[$i]) or die "Can't open $ARGV[$i].";
while (<INPUT>) {
$string = $_;
remove_trailing($string);
remove_leading($string);
print "$string\n";
}
close(INPUT);
}
sub remove_leading {
my($string) = $_[0];
$string =~ s/^\s+//;
$_[0] = $string;
}
sub remove_trailing {
my($string) = $_[0];
$string =~ s/\s+$//;
$_[0] = $string;
}
Related examples in the same category