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\Foo.pl' 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\Foo.pl', 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:
- Can this be cleaned up with regex?
- How can I capture a subdirectory (if it exists) too (without losing current functionality)?