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.

In reply to use File::Basename - need to specify OS? by kiat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.