$SC = "example.pl"; if ($0 =~ m/(.*)$SC/i){ $dir = $1; print $dir."\n";}

$0 outputs the current directory with the filename being ran, which I'd like to match and remove down to the root directory for any includes that are needed.

Lets say $0 = C:\main1\example.pl using the line above it would change to C:\main1\ cutting off the filename which is good.

Now say the same script was ran from C:\main1\sub1\example.pl my $dir output would be C:\main1\sub1\ but I need that to also be C:\main1\ (the first dir in the output)

So my question is can I match or replace the string in a way that no matter how deep its run the output it cuts off anything past the second \ to keep it no deeper than C:\main1\ ?

Basicly any files that need to be written or read will be in the first dir (C:\main1\) and all subdirs need to be able to point there without using chdir specificly to it or inputting a static directory name. Also the lowest dir will not always have a number so it can't match on numeric followed by a slash or anything to that effect.

Please let me know if this is unclear, thanks in advance for any help.


In reply to Perl match to cut off after second occurrence by shadowfox

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.