writing portable file processing scripts that use placeholders in filenames
holli
created: 2005-12-01 03:57:40
In order to compose an answer to [id://513033] i was am about to rewrite a Windows™ only script named ncopy to be platform compatible. With that script I can do stuff like
c:\> ncopy \foo\bar\*.txt \-1\0N.dat\-2\file.dat
which copies
foo\bar\baz.txt to \bar\baz\foo\file.dat
and
foo\bar\xxx.txt to \bar\xxx\foo\file.dat.

Now, how do I handle the "*.txt" cross platform? Under Linux'n'stuff I get a list of files, under Windows I get the string and must glob mysyelf. MacOs? Be? ...? What's common practice?


holli, /regexed monk/
Re: writing portable file processing scripts that use placeholders in filenames
created: 2005-12-01 05:11:25
On OS X and BeOS, and anything else with a proper shell, that will do the globbing for you so you don't have to. I'm not sure about Amiga and VMS.

For Windows, you should probably use File::DosGlob (which is core) or Win32::Wildcard.

Re^2: writing portable file processing scripts that use wildcards in filenames
created: 2005-12-01 11:25:15
AFAIK AmigaDE (or Anywhere, or whatever the hell it's called these days) has a bash-compatible shell (though I'm not sure there's a Perl interpreter for DE). On AmigaOS you'll have to do it yourself, same goes for AROS.
Re: writing portable file processing scripts that use placeholders in filenames
created: 2005-12-01 05:27:46
If you want to do glob processing yourself, you must make sure you get the unexpanded string. But that's something you have to handle in the shell - when it gets to your program it's too late.

It's comparable with doing:

    mysub ("foo$BAR");
in Perl. If you want mysub to deal with the interpolation, you shouldn't change mysub to prevent Perl doing the interpolation. Instead, you change the quotes, or escape the special symbols.

It's the same with a Unix shell. If you want your program to do the globbing, prevent the shell from expanding the *. Use quotes, or a backslash.

Perl --((8:>*
Re: writing portable file processing scripts that use placeholders in filenames
created: 2005-12-01 06:33:30

You might want to look at my module File::Wildcard, which can use regexp captures to derive the new filename. If you want to give this a go, please give me feedback on usability, documentation, etc. and I'm willing to help if you get stuck.

--

Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ?
(Missquoting Janis Joplin)

Re: writing portable file processing scripts that use placeholders in filenames
created: 2005-12-01 07:58:22
I don't know that I'd worry about MacOS. It is old; it hasn't shipped as the primary OS since 2001. All the major apps have been converted/updated to OS X, which is a Unix under the hood. As a former longtime MacOS user, I have completely abandoned it. In fact, I can't remember the last time I've used it, even in the "Classic" emulation mode under OS X. Certainly not since 2002.

Now, while this might be against the philosophy of open source/Perl/CPAN/etc, you might want to ask yourself if it's worth the time and effort.

--
tbone1, YAPS (Yet Another Perl Schlub)
And remember, if he succeeds, so what.
- Chick McGee

Re: writing portable file processing scripts that use placeholders in filenames
created: 2005-12-01 10:52:58
There's no harm in calling glob() on a filename with no wildcards. Just glob all but the last argument. You can use splice() to replace each element with its own globbed list, or you can map the sucker.

--
[ e d @ h a l l e y . c c ]

Re^2: writing portable file processing scripts that use placeholders in filenames
created: 2005-12-01 11:09:50
There's no harm in calling glob() on a filename with no wildcards.
Indeed, there isn't.
Just glob all but the last argument.
No, that would be wrong. Just because globbing on a filename without wildcars is ok doesn't mean that the result of a glob gives you a fixed point.

Suppose for instance you have a utility that removes those files that are given as argument (like rm does). Let's call it remove. But, unlike rm, it globs its arguments - even when working in an environment that globs. Suppose you have a directory that contains the following files:

    thesis_worked_on_for_the_past_four_years
    naked_perl_chicks.tar.gz
    parrot_1.0.0.tar.gz
    [a-z]*
And you want to remove that pesky file named [a-z]*. You call your program like this:
    remove *a-z*
If you had called rm, the first three files would still be there, with the pesky file removed. However, remove, globbing its arguments, will remove the first three files, leaving the pesky file where it is.
Perl --((8:>*
Re^3: writing portable file processing scripts that use placeholders in filenames
created: 2005-12-02 08:42:25
But that's not the syntax of the command the original poster was recalling. rm would glob ALL arguments. cp would glob all but the last argument, since the last argument is defined as the destination.

--
[ e d @ h a l l e y . c c ]

perlmonks.org content © perlmonks.org and DrHyde, halley, holli, kalle, Perl Mouse, rinceWind, tbone1

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03