in reply to clean corpus of ascii not a file
... Perl seems to dislike the use of !.
Not really.
It says void context ...
Not really. Sometimes it's useful to see what Perl thinks of your code. (See O and B::Deparse.)
c:\@Work\Perl>perl -wMstrict -MO=Deparse,-p -le "my $line = qq{xyz\x80\x90\xa0abc}; print qq{'$line'}; ;; $line !~ s/[^[:ascii:]]//g; print qq{'$line'}; " Useless use of negative pattern binding (!~) in void context at -e lin +e 1. BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use strict 'refs'; (my $line = "xyz\200\220\240abc"); print("'${line}'"); (not ($line =~ s/[^[:ascii:]]//g)); print("'${line}'"); -e syntax OK
In this case (as pointed out by GrandFather above), the rather unusual (but syntactically correct) statement
$line !~ s/[^[:ascii:]]//g;
is logically inverting the result produced by the s/// built-in after it finishes operating on the string to which it is bound ($line in this case). This result is the number of substitutions performed, 3 in the case of the code example. The truth of 3 (i.e., true) is then inverted to '' (the empty string), the canonical false value. But this value is then thrown away! Because you asked it to by enabling warnings, Perl is warning you about a "Useless use of ..." some operation, logical inversion in this case. (Granted, the full warning here is a bit puzzling, but it's a very unusual statement. Maybe use-ing diagnostics would be more informative. Try it.)
c:\@Work\Perl>perl -wMstrict -le "my $line = qq{xyz\x80\x90\xa0abc}; print qq{'$line'}; ;; $line !~ s/[^[:ascii:]]//g; print qq{'$line'}; " Useless use of negative pattern binding (!~) in void context at -e lin +e 1. 'xyzÇÉáabc' 'xyzabc'
Try assigning the result of the s/// to a variable with, e.g.,
my $result = $line !~ s/[^[:ascii:]]//g;
or
my $result = $line =~ s/[^[:ascii:]]//g;
(no logical inversion) and see what you get. Happy experimenting!
|
|---|