Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

s[][] notation in perl

Tags:

regex

perl

I have seen this perl code on the cpan documentation for a module:

$filename =~ s[^.+/][];

What does this mean? I am used to the s// notation for the s function.

like image 307
CJ7 Avatar asked Dec 24 '22 08:12

CJ7


1 Answers

Perl allows any non-whitespace character to be used as a string delimiter. See Quote and Quote-like Operators

Bracketing characters are used in pairs, like <...>, [...], (...), and {...}, while single-quotes prevent interpolation. Otherwise the functionality is identical to that of the default delimiter

In this case

$filename =~ s[^.+/][]

is the same as

$filename =~ s/^.+\///

but is far more readable

I tend to prefer the pipe character | which looks similar to the usual slash

$filename =~ s|^.+/||

but the choice is up to you

like image 135
Borodin Avatar answered Dec 27 '22 12:12

Borodin