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, ChrisIn reply to Return value from a binding operator? by Spinone
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |