If you want a single script to use File::Basename on pathname strings that come from different machines, I think you'll need to tell it how to behave for each input string, which means you have to check for backslashes (and this will probably suffice).

You probably want something like this (I've written it so that it expects you to provide one or more pathname strings as command-line args, and these may need to be quoted in some way, depending on what sort of shell you are usng):

#!/usr/bin/perl use strict; use File::Basename; for my $pathname ( @ARGV ) { my $ostype = ( $pathname =~ /\\/ ) ? "MSDOS" : "UNIX"; fileparse_set_fstype( $ostype ); warn "\n parsing $pathname using $ostype rules\n"; my ( $name, $path, $suffix ) = fileparse( $pathname, qr{\.\w+$} ); print "Name: $name\nPath: $path\n.Ext: $suffix\n"; }
Here's a sample run (using a bash shell on a unix box, so single quotes to protect the backslashes from interpretation by the shell):
$ ./test.pl 'c:\foo\bar\baz.exe' /usr/share/doc/bash/bash.pdf parsing c:\foo\bar\baz.exe using MSDOS rules Name: baz Path: c:\foo\bar\ .Ext: .exe parsing /usr/share/doc/bash/bash.pdf using UNIX rules Name: bash Path: /usr/share/doc/bash/ .Ext: .pdf

(update: In case it's not obvious, I'm not suggesting that you should use a separate command-line script from within your CGI code. The point of my script is simply to demonstrate the use of the "fileparse_set_fstype()" function in File::Basename, which is what you need in order to tell the fileparse() function how to behave for a given input. Also, I fixed a spelling error in my script's output.)


In reply to Re: Parsing a filename by graff
in thread Parsing a filename by rooneyl

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.