Spinone has asked for the wisdom of the Perl Monks concerning the following question:
I have inherited a script which contains (amongst other things) a one-line function as follows:
sub Basename { return $0 =~ m!/! ? $0 =~ m!/?([^/]+$)! : $0; }
This gets called from the main program using &Basename in a PRINT statement.
I can see that this is using the Perl ?: Ternary Operator and the m (match) operator with ! delimiters rather than the usual slash.
$0 is the full filename, so the first part of the ternary (the test) checks if the filepath contains a forward slash.
If it DOESN'T (e.g. if it's a Windows filepath which uses backslashes), the full filepath is returned (via the last part of the ternary), BUT if it's a UNIX-like filepath, it returns just the name of the file, stripping out the path - e.g. if the full script path is /opt/bin/mydir/perl/setup.pl, it will return setup.pl
I can see what it's doing, but don't understand how the bind in $0 =~ m!/?([^/]+$)! strips the path off - or, in fact, returns anything.
Can anyone explain what's happening here?
Cheers, Chris
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Return value from a binding operator?
by LanX (Saint) on Apr 12, 2013 at 16:15 UTC | |
by Athanasius (Archbishop) on Apr 12, 2013 at 16:35 UTC | |
by LanX (Saint) on Apr 12, 2013 at 16:57 UTC | |
by Spinone (Novice) on Apr 12, 2013 at 22:47 UTC | |
by LanX (Saint) on Apr 12, 2013 at 23:09 UTC | |
|
Re: Return value from a binding operator?
by Don Coyote (Hermit) on Apr 12, 2013 at 16:21 UTC | |
by Spinone (Novice) on Apr 12, 2013 at 23:07 UTC | |
by dsheroh (Monsignor) on Apr 13, 2013 at 09:50 UTC | |
|
Re: Return value from a binding operator?
by AnomalousMonk (Archbishop) on Apr 13, 2013 at 21:09 UTC |