http://qs1969.pair.com?node_id=1075443

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I need some ideas on how to write perl script, which I should run both in Linux and Windows?

Please give me a reference to go through.


All is well

Replies are listed 'Best First'.
Re: OS independent perl script
by Discipulus (Canon) on Feb 19, 2014 at 12:10 UTC
    first read perlport then you can also Super Search and find the tobyink replay in Writing portable code or read the docs of File::Spec...
    what have you tried until this moment?

    Hth
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: OS independent perl script
by kcott (Archbishop) on Feb 19, 2014 at 13:27 UTC
Re: OS independent perl script
by walto (Pilgrim) on Feb 19, 2014 at 12:29 UTC
    If you are planning a gui application I would recommend using perl/tk.
      I use Tkx myself, but the Tk toolkit is very nice once you wrap your head around it. I've heard good things about wxwidgits as well, but have not taken time to play with it yet.
Re: OS independent perl script
by sundialsvc4 (Abbot) on Feb 19, 2014 at 14:10 UTC

    It might well be easier to talk about what would not be operating-system independent.   All of the core features of the language are implemented in an OS-independent (or, transparently OS-specific) way.   Many (but not all ...) CPAN packages have OS-independence (and there exists a “CPAN Testing Service = CPANTS” which specifically tests them against a very broad gamut of architectures that a package developer might not have access to.)   There are also packages, such as File::Temp, which are specifically designed to let you write things that otherwise would be OS-dependent, in an OS-independent (more or less ...) way.

    Problems will arise in your own applications when you write code “by hand,” e.g. using system() calls directly, and so on.   Or even in more obscure ways, as with case-sensitive vs. case-insensitive filenames.   There is a great deal of information already on the Web in a number of places, not just here.   For example, Google "portable perl code".

    Finally, “the operating system type” is just one specific example of “enviornment dependency.”   You wouldn’t believe how many systems simply outgrow a small server-farm comprised of sneezy, wheezy, and doc, (or gandalf, frodo, and bilbo), and those server-names (and lots of other stuff that was hastily built with no regard for the future) are friggin’ everywhere . . .   It is best to be thinking of portability (and expansion) from the earliest possible moment in a project.

Re: OS independent perl script
by pvaldes (Chaplain) on Feb 19, 2014 at 15:01 UTC

    Just write it, sticking to the core functions, avoid "system()" lines as said, test it on different platforms and Operative systems and if you find troubles ask a specific question here.

    There is a gazillion ways to write a "perl script"; without knowing what problem you want to solve is complicated to know the best way to write and fix it.

Re: OS independent perl script
by DrHyde (Prior) on Feb 21, 2014 at 11:21 UTC

    The hardest bit IMO isn't writing the perl code, it's making it executable on all the bizarro platforms that perl runs on. That is, to get the #! line right on Unix-a-likes, but make it a valid .BAT file on Windows, and whatever is needed on VMS.

    Thankfully, there's a tool to do that.

    ExtUtils::MakeMaker and, presumably, Module::Build, have the smarts to edit your scripts appropriately at install time so you don't have to worry about it. If for some reason you can't use either of those tools, then the maximally portable preamble for Unix-a-likes is:

    #!/bin/sh exec perl -x $0 "$@" #!perl # your perl code goes here

    but I have no idea what to do, or even if you can, make that also portable to other platforms.

      For Windows, there is a really simple .cmd file that I use to launch a plain Perl script with the same name:

      @echo off perl -w %~dpn0.pl

      For a file test.cmd, this will launch perl -w test.pl in the same directory as test.cmd.

        Is %~dpn0 some bizarro shell magic then?