Hello fellow monks,

I've got a command-line script that needs to run on multiple platforms, specifically Windows (both with and without Cygwin) and Un*x. The script takes a list of files and does some munging on them:

munge.pl a.txt b.txt c.txt

The issue arises when I process command-line args that contain wildcards:

munge.pl *.txt *.xml

bash seems to expand the wildcards into matching filenames before passing them to the perl script, whereas Windows just passes the arguments with wildcards intact.

Since I'd like behavior to be consistent (DWIM!) on both platforms, I experimented with using this code at the beginning of my script:

push @files, glob($_) foreach @ARGV;

This appears to have the desired effect: on Windows it forces the wildcards to expand, and on Cygwin/Linux it doesn't change anything because the wildcards have already been expanded.

My question for the wise folks here in the Monastery is: Is this a reasonably safe approach? Or have I opened myself up to sneaky problems I haven't thought about? Are there better ways to accomplish this task? (Search here didn't turn up much, but maybe I'm not asking the question in the right way...)


In reply to portable globbing behavior for command-line tool by seattlejohn

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.