... 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!


In reply to Re: clean corpus of ascii not a file by AnomalousMonk
in thread clean corpus of ascii not a file by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.