|
John W. Krahn |
at Jan 23, 2011 at 8:55 am
|
⇧ |
| |
Peter K. Michie wrote:
I have this regex expression in a script that appears to do an array
like split of a string but I cannot figure out how it does so. Any
help appreciated
$fname = ($0 =~ m[(.*/)?([^/]+)$])[1] ;
print "7 $errlog\n";
$fpath = ($0 =~ m[(.*/)?([^/]+)$])[0] ;
print "8 $errlog\n";
The array elements 0 and 1 above extract the path to the executable
and the executable filename but there is no array definition anywhere
A regular expression with capturing parentheses will return a list of
those captures in list context. The particular item from the list is
extracted via a list slice with one index.
You could combine those two captures and you wouldn't need the list slice:
( $fpath, $fname ) = $0 =~ m[(.*/)?([^/]+)$]
Or, what you should do is use a module designed for this specific task:
use File::Basename;
my $fname = basename( $0 );
my $fpath = dirname( $0 );
Or perhaps use the File::Spec module.
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein