Reputation: 4367
My program is reading in a file, line by line. But if a non printable character is found (ex. characters with decimal value less than 32), then I just want to output an error message to the console.
Here is what I have
while($line=<FILE>){
print $line;
$line =~ s/^\s+//; #remove leading spaces
$line =~ s/\s+$//; #remove trailing spaces
if(/[\x00-\x1F]/){
print "Found a non printable!";
}
I'm not sure if it is working, because I guess I'm not sure what the non printable would be in my file?
This is my current input file:
Pa$$word412
999
Wouldn't a carriage return be non-printable? Then shouldn't my warning message be printed?
Thanks!
Upvotes: 0
Views: 3351
Reputation: 2847
You are reading the line into $line
but not referencing this on your match. You would need to match $line
explicitly like this:
if ($line =~ /[\x00-\x1F]/) {
Or, better yet, just put your line into $_
:
while (<FILE>) {
print;
s/^\s+//; # remove leading spaces
s/\s+$//; # remove trailing spaces
if (/[\x00-\x1F]/) {
print "WARNING -- Non-printables were found; they have been detected.";
}
}
Trailing newlines will not trigger your warning because they match \s
and are stripped by your trailing space removal. If you want a test case, put a tab character in your file; this should then match [\x00-\x1F]
.
Upvotes: 2
Reputation: 118605
A carriage return is non-printable, by your definition; but you are removing the carriage return from your input when you say
$line =~ s/\s+$//; #remove trailing spaces
Upvotes: 1