in reply to Getting the filename from full path name ?

For portability's sake, use the File::Spec or File::Basename module (because different OSs have different rules for what separates the path, and such). However, in answer to the general question of how one does this sort of string manipulation in perl:
my @path_pieces = split('/', $path); my $last_path_piece = $path_pieces[$#path_pieces];
or
my ($last_path_piece) = $path =~ m|/(.*?)$|;
my ($last_path_piece) = $path =~ m|([^/]*)$|;
to give a couple of the more idiomatic methods.
------------ :Wq Not an editor command: Wq

Replies are listed 'Best First'.
Re: Re: Getting the filename from full path name ?
by satchm0h (Beadle) on Apr 15, 2004 at 17:09 UTC
    Your second solution matches the first / in the path. As Chris stated above you probably would rather do:

    my ($file) = $a =~ m|/([^/]+)$|;

    satchm0h
      Yes, sorry. I made the classic mistake of assuming that non-greedy matching was, in fact, non-greedy. In general, I avoid the use of non-greedy matching (instead, being explicit about what I don't want the match to bleed into) for this basic reason. I was just being sloppy.
      ------------ :Wq Not an editor command: Wq