Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Need to capture an optional sub-directory name

by technojosh (Priest)
on Mar 29, 2012 at 15:18 UTC ( #962426=perlquestion: print w/replies, xml ) Need Help??
technojosh has asked for the wisdom of the Perl Monks concerning the following question:

I have a sub I use to build a name of a "test case" during an automation run. This name is a string that includes the directory of the Perl .pl file, as well as the file name with the suffix (.pl) stripped off.

So a file with the path 'C:\Tools\scripts\Test\' would produce the string 'Test - Foo'

here is my current sub, which works for me as long as there is no directory between 'Test' and 'Foo':

sub ScriptName { my ($self) = @_; my $name = Win32::GetFullPathName($0); my $dirChop = substr( $name, index($name,'\\scripts\\')+9 ); my $dir = substr( $dirChop, 0, index($dirChop,'\\') ); my $clean = substr( $name, rindex($name,'\\')+1, rindex($name,'.')-(rindex($name,'\\')+1) ); return "$dir - $clean" }

This will totally ignore the sub-directory in the path 'C:\Tools\scripts\Test\Unstable\', where I would like it to return the string 'Test - Unstable - Foo'... but I am no regex pro.

I am looking for a couple pointers:
  1. Can this be cleaned up with regex?
  2. How can I capture a subdirectory (if it exists) too (without losing current functionality)?

Replies are listed 'Best First'.
Re: Need to capture an optional sub-directory name
by BrowserUk (Pope) on Mar 29, 2012 at 15:28 UTC
    but I am no regex pro

    You don't need to become a "regex pro" -- whatever one of those is -- but you would sure save yourself some time in the long run, by making the effort to learn the basics:

    $p = 'C:\Tools\scripts\Test\Unstable\';; $p =~ s[C:\\Tools\\scripts\\([^.]+).pl][$1]; $p =~ s[\\][ - ]g; print $p;; Test - Unstable - Foo $p2 = 'C:\Tools\scripts\Test\';; $p2 =~ s[C:\\Tools\\scripts\\([^.]+).pl][$1]; $p2 =~ s[\\][ - ]g; print $p2;; Test - Foo

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: Need to capture an optional sub-directory name
by spazm (Monk) on Mar 29, 2012 at 16:26 UTC
    You way want to look into a module like File::Spec or Path::Class that allows manipulation of file paths to use the fact that you know this is a path situation.
    use File::Spec sub SriptName { my ($self) = @_; my $name = Win32::GetFullPathName($0); my ($volume, $dirs, $file ) = File::Spec->splitpath( $name ); my (@dirs) = File::Spec->splitdir( $dirs ); $_ = shift @dirs until $_ eq 'scripts'; #drop all suffix (not sure if this is what you want here) # this could/should also be $file = basename($file) $file =~ s/\..*//; return join( ' - ', @dirs, $file) }
Re: Need to capture an optional sub-directory name
by AnomalousMonk (Chancellor) on Mar 29, 2012 at 18:35 UTC

    A nice module like File::Spec or Path::Class will probably save you some head-hurt, but here's a possible regex approach (needs 5.10+ for \K):

    >perl -wMstrict -le "my @paths = qw( C:\Tools\scripts\Test\ C:\Tools\Test\Unstable\ C:\Tools\scripts\Test\Bar.dir\Baz\ C:\Tools\Test\Baz\Test\ ); ;; my $all_before_Test = qr{ \A (?: (?! \\ Test) .)* }xms; ;; for my $path (@paths) { print qq{'$path'}; my @chunks = $path =~ m{ (?: $all_before_Test | \G) \\ \K [^\\]+ }xmsg; $chunks[-1] =~ s{ [.] [^.]* \z }{}xms; my $msg = join q{ - }, @chunks; print qq{'$msg' \n}; } " 'C:\Tools\scripts\Test\' 'Test - Foo' 'C:\Tools\Test\Unstable\' 'Test - Unstable - Foo' 'C:\Tools\scripts\Test\Bar.dir\Baz\' 'Test - Bar.dir - Baz - Foo' 'C:\Tools\Test\Baz\Test\' 'Test - Baz - Test - Foo'

    If you want 'C:\Tools\Test\Baz\Test\' to render as 'Test - Foo' use
        my $all_before_Test = qr{ \A .* (?= \\ Test) }xms;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://962426]
Approved by ww
Eily went to the wiktionary to check that the pronunciations of prawn and prone are actually similar. Turns out prawn can be an alternative form of porn...
[Discipulus]: well i'm still at the end of the food chain..
[Discipulus]: so a new life as krill in prague will be good
[erix]: wtf -- what's the idea, is this trolling, advertising, or what?
[Discipulus]: but realisticly choroba your company would a hire a programmer as me?
Discipulus spent ~3 hours on the roof.. with mediocre results..

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2017-06-29 12:50 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (663 votes). Check out past polls.