FAQ
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

Search Discussions

  • 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
  • Peter K. Michie at Jan 26, 2011 at 2:16 am

    On Jan 23, 2:55 am, [email protected] ("John W. Krahn") wrote:
    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
    Thank you for the reply.
    Much appreciated

    Peter

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJan 21, '11 at 3:46p
activeJan 26, '11 at 2:16a
posts3
users2
websiteperl.org

2 users in discussion

Peter K. Michie: 2 posts John W. Krahn: 1 post

People

Translate

site design / logo © 2023 Grokbase