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:

  1. It's easy to build a solution that looks like it works, but isn't general
  2. ww's command of lookarounds needs improvement

In reply to Re^3: get file name? by ww
in thread get file name? by adrive

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.