Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I agree with others that considerably simplified versions of your function are available and IMHO preferable.

I want to comment on one test in your original post. The
    elsif ( $string =~ m/[#@':;><,.{}[]=!"£$%^&*()]/ ) { ... }
test is wrong/useless.

+---------------------------- start of character class | +--------------- END of character class !!! | | | |+-------------- characters and metacharacters | || from here on | || | || +-------- START-OF-STRING anchor !!! | || | | || | +------ 0-or-more quantifier | || | | | || | |++---- empty capture group | || | ||| | || | |||+--- literal ] character | || | |||| v vv v vvvv m/[#@':;><,.{}[]=!"£$%^&*()]/ ^ ^ | | | +--- interpolated scalar $% | +------------------- interpolated array @'
The premature end of the character class makes the regex nonsense, but most egregiously it produces a ^ start-of-string anchor metacharacter that requires characters before it (i.e., before the start of the string) for a match to occur. Thus, no match can ever occur.

Beyond that, there are two interpolated global special variables that appear in the originally posted regex and cause it to be other than what one might expect. See perlvar for $%.   @' is the unused array slot of the $' regex special variable typeglob (see also perlvar).

Win8 Strawberry 5.8.9.5 (32) Sat 12/18/2021 23:35:28 C:\@Work\Perl\monks >perl use strict; use warnings; use Data::Dump qw(dd); my $regex = qr/[#@':;><,.{}[]=!"£$%^&*()]/; # as posted pm#11139686 Possible unintended interpolation of @' in string at - line 7. # my $regex = qr/[#\@':;><,.{}[\]=!"£\$%^&*()]/; # corrected dd $regex; no warnings 'qw'; for my $c (qw/ # @ ' : ; > < , . { } [ ] = ! " £ $ % ^ & * ( ) /) { print "'$c' ", $c =~ $regex ? ' ' : 'NO', " match \n"; } ^Z qr/[#:;><,.{}[]=!"£0^&*()]/ '#' NO match '@' NO match ''' NO match ':' NO match ';' NO match '>' NO match '<' NO match ',' NO match '.' NO match '{' NO match '}' NO match '[' NO match ']' NO match '=' NO match '!' NO match '"' NO match '£' NO match '$' NO match '%' NO match '^' NO match '&' NO match '*' NO match '(' NO match ')' NO match
Note that @' and $% do not appear in the dd dump of the regex – and where does the 0 come from?

Fixing the character class termination and suppressing variable interpolation yields the results I think you want:

Win8 Strawberry 5.8.9.5 (32) Sat 12/18/2021 23:36:37 C:\@Work\Perl\monks >perl use strict; use warnings; use Data::Dump qw(dd); # my $regex = qr/[#@':;><,.{}[]=!"£$%^&*()]/; # as posted pm#11139686 my $regex = qr/[#\@':;><,.{}[\]=!"£\$%^&*()]/; # corrected dd $regex; no warnings 'qw'; for my $c (qw/ # @ ' : ; > < , . { } [ ] = ! " £ $ % ^ & * ( ) /) { print "'$c' ", $c =~ $regex ? ' ' : 'NO', " match \n"; } ^Z qr/[#\@':;><,.{}[\]=!"£\$%^&*()]/ '#' match '@' match ''' match ':' match ';' match '>' match '<' match ',' match '.' match '{' match '}' match '[' match ']' match '=' match '!' match '"' match '£' match '$' match '%' match '^' match '&' match '*' match '(' match ')' match
Note that the @' and $% literal sequences are in their proper places in the dd dump of the regex.

Same results running under Strawberry Perl 5.30.3.1 except the Possible unintended interpolation of @' in string ... warning is not emitted.

Update: If you really need to do something like
    $string =~ m/[#\@':;><,.{}[\]=!"£\$%^&*()]/;
to test for the presence of certain characters, it might be better to use tr (see also in perlop):
    $string =~ tr/#@':;><,.{}[]=!"£$%^&*()//;
(in scalar or boolean context). I suggest tr// because it has fewer features (e.g., variable interpolation, character classes, etc.) than m// or s/// and so is less prone to mistakes and misinterpretation.

Win8 Strawberry 5.8.9.5 (32) Mon 12/20/2021 17:08:37 C:\@Work\Perl\monks >perl use strict; use warnings; no warnings 'qw'; for my $c (qw/ # @ ' : ; > < , . { } [ ] = ! " £ $ % ^ & * ( ) 1.2 0 1 2 - + -1 +1 12 +12 -12 /) { my $is_blacklisted = $c =~ tr/#@':;><,.{}[]=!"£$%^&*()//; print "'$c'", $is_blacklisted ? '' : ' NOT', " blacklisted \n"; } ^Z '#' blacklisted '@' blacklisted ''' blacklisted ':' blacklisted ';' blacklisted '>' blacklisted '<' blacklisted ',' blacklisted '.' blacklisted '{' blacklisted '}' blacklisted '[' blacklisted ']' blacklisted '=' blacklisted '!' blacklisted '"' blacklisted '£' blacklisted '$' blacklisted '%' blacklisted '^' blacklisted '&' blacklisted '*' blacklisted '(' blacklisted ')' blacklisted '1.2' blacklisted '0' NOT blacklisted '1' NOT blacklisted '2' NOT blacklisted '-' NOT blacklisted '+' NOT blacklisted '-1' NOT blacklisted '+1' NOT blacklisted '12' NOT blacklisted '+12' NOT blacklisted '-12' NOT blacklisted
Also same results under Strawberry Perl 5.30.3.1.


Give a man a fish:  <%-{-{-{-<


In reply to Re: Is A Number (updated) by AnomalousMonk
in thread Is A Number 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found