Why is the # character considered a word character in Perl?

Why is the # character considered a word character in Perl? Or I don’t understand how this code should work?

#!/usr/bin/perl

my $filename = "Something_with_#_sign.jpg";

$filename =~ s/        # substitute...
                [^             # characters which are NOT:
                \w                # "word" characters
                ]              # end of character classes
                /_/xg;     # ...with an underscore

print "$filename\n";

Productivity:

Something_with_#_sign.jpg

I would expect the # sign to be replaced by the _ symbol (underscore).

+4
source share
1 answer

/xdoes not change the syntax of character classes (or \x20, or s{3,4}, etc., etc.), therefore

[^             # characters which are NOT:
\w                # "word" characters
]              # end of character classes

is a weird way of recording

[^ "#:NOTacdefhilnorst\n\w]
+10
source

Source: https://habr.com/ru/post/1609523/


All Articles