in reply to Re: Getting the filename from full path name ?
in thread Getting the filename from full path name ?

Actually File::Basename is to say bluntly a turd. You are much better off using File::Spec and or the functional equivelents in File::Spec::Functions. File::Basename was effectvely deprecated when File::Spec was created (which it was to fix a whole bunch of know problems in the Basename, some outstanding to this day) but it never was marked as such or removed for hysterical porpoises. File::Spec is in the core and it should be used as a general rule for manipulating paths and directories and the like.

Anyway, the File::Spec::Functions way would be:

use File::Spec::Functions qw(splitpath); my (undef,undef,$file)=splitpath($path);
Or:
use File::Spec; my ($vol,$path,$file)=File::Spec->splitpath($path);

Also IMO its not worth ignoring the $vol just becuase _your_ OS doesnt have such a concept. If you use File::Spec and assume there will be a volume then everything will work out regardless of where youtr code ends up.


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi


Replies are listed 'Best First'.
Re: Re: Re: Getting the filename from full path name ?
by eserte (Deacon) on Apr 15, 2004 at 13:03 UTC
    It would be nice if File::Spec had a basename function, too. E.g. sub basename { (splitpath($_[0]))[-1] }. And probably also dirname.

      Why does it need one if it has splitpath() ?


      ---
      demerphq

        First they ignore you, then they laugh at you, then they fight you, then you win.
        -- Gandhi


        • Fewer characters to type
        • I immediately see what is meant
        • There already is File::Basename::basename, and there's the unix utility called basename, hence it would be nice to have a function with the same name in the Perl standard library
        Let me second esserte's comment.

        Why do we need push() when we have splice(@a,@a,0,$x,$y)? Because primordial features, while powerful, are not always expressive in terms the reader will immediately recognize.

        --
        [ e d @ h a l l e y . c c ]