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

Hi monks,

I discovered "use File::Basename" through the responses I got from a node of mine use CGI vs use CGI qw(:standard). I've the following code which works on a Windows machine running apache 2++:

use CGI; use File::Basename; my $q = new CGI; # $file set to 'C:\Windows\images\hello.gif' my $file = $q->param('file'); # $filename set to hello.gif my $filename = basename($file);
That works. The same piece of code when run on a BSD server "broke" - I needed to add an extra line to get it to work. So the code above was changed to:
use CGI; use File::Basename; my $q = new CGI; # $file set to 'C:\Windows\images\hello.gif' my $file = $q->param('file'); # Added this line fileparse_set_fstype("MSDOS"); # $filename set to hello.gif my $filename = basename($file);
I'm curious why I needed to add the line "fileparse_set_fstype("MSDOS"). I had the impression the function "basename" would automatically detect the type of OS the file comes from and perform the necessary parsing, so you need not worry about the source of the file.

Could someone enlighten me? Thanks :)

Update:

Change the slashes to '\' instead of '/', as pointed out by b10m.

Update2:

On use CGI vs use CGI qw(:standard), The code by jeffa had a line to detect OS:

fileparse_set_fstype('MSDOS') if $name =~ /\\/;
Which shows that OS detection isn't automatic.

Replies are listed 'Best First'.
Re: use File::Basename - need to specify OS?
by b10m (Vicar) on Apr 21, 2004 at 06:52 UTC

    No prob here if you use File::Basename (I don't know File::Base)

    $ cat foo use File::Basename; $\="\n"; print $^O; $file = 'C:/Windows/images/hello.gif'; $filename = basename($file); print $filename; $ perl foo freebsd hello.gif
    --
    b10m

    All code is usually tested, but rarely trusted.
      Thanks, b10m!

      I had a typo there (I've edited it). In my code, I've 'use File::Basename' and I need the line 'fileparse_set_fstype("MSDOS");' to get it to work.

        Tested it on FreeBSD, OpenBSD and Linux now, still no prob without setting the fileparse_set_fstype.

          FreeBSD OpenBSD Linux
        $^O freebsd openbsd linux
        File::Basename version 2.71 2.71 2.72
        Perl version 5.8.0 5.8.0 5.8.2
        --
        b10m

        All code is usually tested, but rarely trusted.
don't be afraid of File::Spec :)
by PodMaster (Abbot) on Apr 21, 2004 at 07:07 UTC
    Not exactly like File::Basename, but I never use a suffixlist
    sub File::Spec::dirname { shift; return File::Spec->catpath( ( File::Spec->splitpath(shift) )[ 0, 1 + ] ); } sub File::Spec::basename { shift; return File::Spec->catpath( ( File::Spec->splitpath(shift) )[ -1 ] + ); }

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: use File::Basename - need to specify OS?
by pelagic (Priest) on Apr 21, 2004 at 07:29 UTC
    Same as PodMasta ... I usually do it like:
    my (undef,undef,$file) = File::Spec->splitpath( $path_and_file );
    ... works on all OS.

    pelagic
      This depends on the originating OS of the given $path_and_file. If I remember correctly, then the path given through a CGI file upload may be in MSDOS format (with backslashes). Processing this filename with basename or splitpath on a Unix system would give wrong results.

      So if you know the path is a MSDOS path, you should use File::Spec::Win32->splitpath instead of File::Spec->splitpath. See here:

      use File::Spec; warn ((File::Spec->splitpath("c:\\foo\\bar\\basename"))[-1]);
      would return c:\foo\bar\basename at ... on a Unix system, while
      use File::Spec::Win32; warn ((File::Spec::Win32->splitpath("c:\\foo\\bar\\basename"))[-1]);
      would return basename at ... on all systems.
        You're completely right when the path originated not from the actual os.
        In that case I'd probably do it a bit primitive like that:
        ($progname = "c:\\foo\\bar\\basename") =~ s!^.*[\\/]!!; warn $progname;

        pelagic
Re: use File::Basename - need to specify OS?
by saintbrie (Scribe) on Apr 21, 2004 at 10:41 UTC

    From the docs:

    "If you haven't called fileparse_set_fstype(), the syntax {that is, how the path is parsed} is chosen by examining the builtin variable $^O according to these rules."

    $^O is the variable that contains the name of the operating system under which perl was built.

    "these rules" can be found in the perldoc for File::Basename under fileparse_set_fstype.

      And if you're on a Un*x variety, backslashes in your path designators are not appropriate. Thus, your "DOS"-style pathnames are not going to get parsed correctly unless you override your fileparse_set_fstype().

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

Re: use File::Basename - need to specify OS?
by BuddhaNature (Beadle) on Apr 22, 2004 at 03:13 UTC
    Not sure if it is exactly what you are asking, but you can get the "OS name" from $^O... I actually just used that in a script of mine...

    Hope it helps.

    -Shane