This is a fairly difficult topic to broach, because a lot of people here are on Unix style systems and are likely to blow off the whinging windows user. This is unfortunate, because there are a lot more OSs out there than windows and Unix. You want your code to run cleanly on all of them, right? After all you're programming Perl, not Perl/Linux, right? I'm appealing to your pride here.

There isn't much you have to be aware of, but you may feel a little discomforted by having to give up some amazingly cool shell tricks. I think there are replacements for almost all of them.

Don't make the shell do work that Perl can

The favourite for most Unix coders is to use shell tricks to loop over the files in a directory, like this
while (<>) { do_stuff_to $_; } or for (@ARGV) { do_stuff_to $_; }
and insist the the user run:
perl parsedir.pl /home/me/*

The shell (bash, sh, ksh, etc.) expands the * into the list of filenames and hands it to the your program. Unfortunatly DOS/Windows doesn't. You can't rely on the shell working correctly under *nix anyway. A system manager may decide to restrict scripts to running on some primitive shell for memory or security reasons. Use

while (<*>) { do_stuff_to $_; } or better opendir DH,"."; @dir=readdir DH; closedir DH; foreach (@dir) { Do_your_thing $_; }
See here for a nice example.

Don't include specific filesystem symbols

A quick way to do temporary files is:
open (FH, ">/temp/tempfile"); print FH $temporary_data;

except that it breaks DOS machines.

DOS/Windows coders can get their revenge by using
open FH "C:\temp";

You shouldn't be using hard-coded parameters in your program anyway. It will bite you later when you realise you need the temporary file in a different place, and have to go trolling through your code to find all the places you hard-coded it. This is easily solved by placing:

my $tempdir = "/temp/"; my $tempfile = "tempfile"; and then open FH "$tempdir$tempfile";

Always finish your directory names with the directory symbol (forwards or backwards slash or whatever).

Don't use external utilities

Perl has a perfectly good regexp engine. It's probably a lot better than grep. If you're running perl, don't shell out to grep:
`ls -lR | grep myfile`;
is an abomination.

The ls -lR is a problem, because it does a directory recursion much more easily than File::Find. If you're doing a serious script though, you should be File::Find. There's even a wrapper for it now, so it's easier to use.

I hope these few examples have given you a bit of direction when it comes to making your scripts cross-platform. I'm not saying your three-line file parser should be platform independent, but if you're submitting a one-page program to the monastery it would be nice if the windows users could cut'n'paste it too.

____________________
Jeremy
I didn't believe in evil until I dated it.


In reply to Cross platform compatability Part 1: Shells and Files by jepri

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.