What is the meaning of below statement in perl?
($script = $0) =~ s#^.*/##g;
I am trying to understand the operator =~ along with the statement on the right side s#^.*/##g.
Thanks
It's very, very simple:
Perl quote-like expressions can take many different characters as part separators. The separator right after the command (in this case, the s
) is the separator for the rest of the operation. For example:
# Out with the "Old" and "In" with the new
$string =~ s/old/new/;
$string =~ s#old#new#;
$string =~ s(old)(new);
$string =~ s@old@new@;
All four of those expressions are the same thing. They replace the string old
with new
in my $string
. Whatever comes after the s
is the separator. Note that parentheses, curly braces, and square brackets use parings. This works out rather nicely for the q
and qq
which can be used instead of single quotes and double quotes:
print "The value of \$foo is \"foo\"\n"; # A bit hard to read
print qq/The value of \$foo is "$foo"\n/; # Maybe slashes weren't a great choice...
print qq(The value of \$foo is "$foo"\n); # Very nice and clean!
print qq(The value of \$foo is (believe it or not) "$foo"\n); #Still works!
The last still works because the quote like operators count opening and closing parentheses. Of course, with regular expressions, parentheses and square brackets are part of the regular expression syntax, so you won't see them so much in substitutions.
Most of the time, it is highly recommended that you stick with the s/.../.../
form just for readability. It's what people are use to and it's easy to digest. However, what if you have this?
$bin_dir =~ s/\/home\/([^\/]+)\/bin/\/Users\/$1\bin/;
Those backslashes can make it hard to read, so the tradition has been to replace the backslash separators to avoid the hills and valleys effect.
$bin_dir =~ s#/home/([^/]+)/bin#/Users/$1/bin#;
This is a bit hard to read, but at least I don't have to quote each forward slash and backslash, so it's easier to see what I'm substituting. Regular expressions are hard because good quote characters are hard to find. Various special symbols such as the ^
, *
, |
, and +
are magical regular expression characters, and could probably be in a regular expression, the #
is a common one to use. It's not common in strings, and it doesn't have any special meaning in a regular expression, so it won't be used.
Getting back to your original question:
($script = $0) =~ s#^.*/##g;
is the equivalent of:
($script = $0) =~ s/^.*\///g;
But because the original programmer didn't want to backquote that slash, they changed the separator character.
As for the:
($script = $0) =~ s#^.*/##g;`
It's the same as saying:
$script = $0;
$script =~ s#^.*/##g;
You're assigning the $script
variable and doing the substitution in a single step. It's very common in Perl, but it is a bit hard to understand at first.
By the way, if I understand that basic expression (Removing all characters to the last forward slash. This would have been way cleaner:
use File::Basename;
...
$script = basename($0);
Much easier to read and understand -- even for an old Perl hand.
=~
applies the thing on the right (a pattern match or search and replace) to the thing on the left. There's lots of documentation about =~
out there, so I'm just going to point you at a pretty good one.
There's a couple of idioms going on there which are not obvious nor well documented which might be tripping you up. Let's cover them.
First is this...
($copy = $original) =~ s/foo/bar/;
This is a way of copying a variable and performing a search and replace on it in a single step. It is equivalent to:
$copy = $original;
$copy =~ s/foo/bar/;
The =~
operates on whatever is on the left after the left hand code has been run. ($copy = $original)
evaluates to $copy
so the =~
acts on the copy.
s#^.*/##g
is the same as s/^.*\///g
but using alternative delimiters to avoid Leaning Toothpick Syndrome. You can use just about anything as a regex delimiter. #
is common, though I think its ugly and hard to read. I prefer {}
because they balance. s{^.*/}{}g
is equivalent code.
Unrolling the idioms, you have this:
$script = $0;
$script =~ s{^.*/}{}g;
$0
is the name of the script. So this is code to copy the name of the script and strip everything up to the last slash (.*
is greedy and will match as much as possible) off it. It is getting just the filename of the script.
The /g
indicates to perform the match on the string as many times as possible. Since this can only ever match once (the ^
anchors it to the beginning of the string) it serves no purpose.
There's a better and safer way to do this.
use File::Basename;
$script = basename($0);
In perl, you can use many kinds of characters as quoting characters (string, regular expression, list). lets break it down:
$script
variable the contents of $0
(the string that contains the name of the calling script.)=~
character is the binding operator. It invokes a regular expression match or a regex search and replace. In this case, it matches against the new variable, $script
.s
character indicates a search and replace regex.#
character is being used as the delimiter for the regex. The regex pattern quote character is usually the /
character, but you can use others, including #
in this case.^.*/
. It means, "at the start of string, search for zero or more characters until a slash. This will keep capturing on each line except for newline characters (which .
does not match by default.)#
indicating the start of the 'replace' value. Usually you have a pattern here that uses any captured part of the first line.#
again. This ends the replace pattern. Since there was nothing between the start and end of the replace pattern, everything that was found in the first is replaced with nothing.g
, or global match. The search and replace will keep happening as many times as it matches in the value.Effectively, searches for and empties every value before the / in the value , but keeps all the newlines, in the name of the script. It's a really lazy way of getting the script name when invoked in a long script that only works with a unix-like path.
If you have a chance, consider replacing with File::Basename
, a core module in Perl:
use File::Basename;
# later ...
my $script = fileparse($0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With