Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found