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":



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