I think nikil.patil's regex has some shortcomings:
For example, if executed from a directory OTHER than the one containing the script, the path is NOT stripped. On windows (which is all I have readily available today) execution looks like this:
C:\>perl f:\_wo\pl_test\669947.pl
$0 is: f:\_wo\pl_test\669947.pl
nikhil.patil's $2 is: f:\_wo\pl_test\669947.pl
(The code for the above is part of the code block which follows this paragraph.)
So, in the interest of posting a reply with more than mere criticism, I tried several approaches, including several variations on lookarounds, of which this is one that failed less spectacularly than others:
#!C:/Perl/bin -w
print "\n\t\$0 is: " . $0 . "\n\n";
# nikhil.patil's regex
$0 =~ /(.*\/)*(.+)/;
print "\n\tnikhil.patil's \$2 is: $2\n\n";
print "\n\nww's failed\n\n";
if ( $0 =~ /(?!(.*?)\Q\\\E)\G(.+)$/ ) {
print "\n\t \$1 is: |$1|. However, note my failure to capture the
+final '\\' in the path\n";
# $path = $1;
# print "\n\t\$path is: $path\n"; # BUT HOW DO I CAPTURE THE LAST
+ "\" IN THE PATH
print "\n\t \$2 is: |$2| \n";
print "\n\t \$2 is the whole of \$0 because lookarounds, when comp
+leted, discard all intermediate info\n";
} else {
print "\n\tNo match\n";
}
which definitely doesn't do the job:
ww's failed
$1 is: |f:\_wo\pl_test|. However, note my failure to capture
+the final '\' in the path
$2 is: |f:\_wo\pl_test\669947.pl|
$2 is the whole of $0 because lookarounds, when completed, di
+scard all intermediate info
So, using substitution (After Friedl p192)
$foo = $0;
print "\n\t \$foo: |$foo| before the substitution \n";
$foo =~s/^.*\\//;
print "\n\t And, after the substitution: |$foo| \n";
produces this...
$foo: |f:\_wo\pl_test\669947.pl| before the substitution
And, after the substitution: |669947.pl|
C:\>
There are (at least) two morals to this story:
- It's easy to build a solution that looks like it works, but isn't general
- ww's command of lookarounds needs improvement
|