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.
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.+---------------------------- 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 @'
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).
Note that @' and $% do not appear in the dd dump of the regex – and where does the 0 come from?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
Fixing the character class termination and suppressing variable interpolation yields the results I think you want:
Note that the @' and $% literal sequences are in their proper places in the dd dump of the regex.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
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.
Also same results under Strawberry Perl 5.30.3.1.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
Give a man a fish: <%-{-{-{-<
In reply to Re: Is A Number (updated)
by AnomalousMonk
in thread Is A Number
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |