Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to write a perl script to work on many OS(s)

by jesuashok (Curate)
on Jul 06, 2007 at 03:37 UTC ( #625176=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I have a perl scrit which accepts directory path and filenames through different argument names. For example :-

perl script_name.pl -dir "/home/test/input_dir" -file input_file_1 inp +ut_file_2 input_file_3
In my script I would open the files as follows :-
foeach my $file_names ( @file_names_passed ) { open ( FH , "$dir_path/$file_name) or die "$!"; # managed to do some operation on the file. close(FH); }

But, if I run the same script in windows OS, I have to specify "\\" for delimiting the path. ( unix it is "/" ).
Is there any common character available to delimit the path regardless of what OS I am using this script ?

Apologies If I anyone got irked because of this question.



i m possible

Comment on How to write a perl script to work on many OS(s)
Select or Download Code
Re: How to write a perl script to work on many OS(s)
by bobf (Monsignor) on Jul 06, 2007 at 03:57 UTC

    Take a look at File::Spec - it does exactly what you're looking for. You may also find File::Basename and the other File::* modules interesting. Take a walk through CPAN when you have time - it's a great way to find existing solutions to problems you may not have even run into yet.

    Secondly, see perlport for a more general discussion on writing portable Perl.

Re: How to write a perl script to work on many OS(s)
by xdg (Monsignor) on Jul 06, 2007 at 03:58 UTC

    See File::Spec.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: How to write a perl script to work on many OS(s)
by GrandFather (Cardinal) on Jul 06, 2007 at 04:00 UTC
Re: How to write a perl script to work on many OS(s)
by parv (Priest) on Jul 06, 2007 at 04:10 UTC

    Could you tell us/me how do you parse the options? Is your option parsing robust enough that it could handle options given in any order or be specified multiple times? Does the "script_name.pl" POD document option usage and order?

Re: How to write a perl script to work on many OS(s)
by swampyankee (Parson) on Jul 06, 2007 at 04:58 UTC

    Use forward slashes (aka virgules). Generally, Perl will treat / as the path delimiter in Windows; see here. Quoting: "Since all DOS and Windows versions since something like MS-DOS 2.0 or so have treated / and \ the same in a path, you might as well use the one that doesn't clash with Perl--or the POSIX shell, ANSI C and C++, awk, Tcl, Java, or Python, just to mention a few. POSIX paths are more portable, too." (perlfaq5)

    Note that different shells have different quoting conventions, and different methods of determining when variable substitution should take place.

    emc

    Any New York City or Connecticut area jobs? I'm currently unemployed.

    There are some enterprises in which a careful disorderliness is the true method.

    —Herman Melville

      Fine for *nix and DOS/Windows, but not so good for Mac and others maybe?


      DWIM is Perl's answer to Gödel
        For Mac OS 9 and older you should use ":" as a path separator, but Mac OS X uses "/" just fine.
      Forward slashes generally work in Perl MSWin32. There are a few corner cases where this doesn't work. ALSO... don't forget that windows shares need to start with two slashes... I believe File::Spec::canonpath will collapse them down to one, causing problems.

        Thanks; I wasn't aware of File::Spec::canonpath collapsing leading double slashes to one. I'll watch out for that in the future.

        emc

        Any New York City or Connecticut area jobs? I'm currently unemployed.

        There are some enterprises in which a careful disorderliness is the true method.

        —Herman Melville
Re: How to write a perl script to work on many OS(s)
by jhourcle (Prior) on Jul 06, 2007 at 14:12 UTC

    For the larger issue of making the program work in multiple OSes, you might want to read perlport.

Re: How to write a perl script to work on many OS(s)
by adrianh (Chancellor) on Jul 08, 2007 at 10:33 UTC

    As well as File::Spec you might also want to take a look at Path::Class. It's not core, but has a much saner API in my opinion.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://625176]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-12-18 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls