Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I if have a path of "/tmp/test/t1.out". I want just want the "t1.out" section of the path. How can I do this?

Replies are listed 'Best First'.
Re: split "/" out of path
by Limbic~Region (Chancellor) on Apr 01, 2004 at 19:33 UTC
    Anonymous Monk,
    If you only want t1.out of the path, there is no reason to split.
    use File::Basename 'basename'; my $file = basename( "/tmp/test/t1.out" );
    Cheers - L~R
Re: split "/" out of path
by hardburn (Abbot) on Apr 01, 2004 at 19:28 UTC

    Simple way: m! ([^/]+) \z!x;, and the $1 will contain what you want. However, this is not crossplatform and you never know what sort of subtle problems it will cause. For a less fragile solution, look at File::Spec->splitdir.

    ----
    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: split "/" out of path
by guha (Priest) on Apr 01, 2004 at 20:06 UTC

    If you really want to split...

    #!perl -w my $string = "/tmp/test/t1.out"; my $file = (split("/", $string))[-1]; print $file, $/; __END__ t1.out
    HTH