At 7:30 AM -0400 4/18/02, Chris Nandor wrote:
>At 11:40 +0200 2002.04.18, Thomas Wegner wrote:
>>Noooooooo! Don't abuse File::Spec.
>
>Amen, halleluja, preach it brother!
>
>Just to repeat: knowing the path separator is almost never useful toward
>the end of producing portable code. File::Spec most likely does whatever
>you need without knowing it. And further, even if you wanted to write
>portable code that way, you couldn't do it: VMS doesn't have the same
>notion of path separators that Mac and Unix have. You would wind up just
>reimplementing File::Spec itself. :-)
You're right. Not to seem argumentative, here's a case where I didn't
find a way to avoid conjuring a local path separator:
I wrote a small all-Perl utility to prevent line-ending confusion as
I fling files among Mac Classic, OS X-darwin/UNIX/Linux, and
NT/Win32/DOS systems during development, testing, and deployment. I
guess I'm lucky not to have VMS in the picture :-)
The core of my utility is Perl's "inplace" file edit with the -i
switch, or as I use it, the special variable $^I.
A typical usage of $^I:
local $^I = '.bak';
which creates a copy of the original file, with '.bak' appended to
the filename.
There's also:
local $^I = "$another_dir/*";
which names the copy with $another_dir prepended to the filename
(symbolized by '*').
This is handy because my utility can extract files matched by some
criteria and put them in a separate directory for line-end conversion.
But that slash before the '*' became a problem when I happened to use
my utility from within MacPerl.
Excerpts from my solution:
# A low-cycle hack to get a probable path separator
my $SEP = "\n" eq "\012" ? '/' : # *N*X
"\n" eq "\015" ? ':' : # Mac
"\n" eq "\015\012" ? '\' : # DOS/Win ('/' often works)
'/' ; # default
# Duplicate into $DESTINATION_DIR acceptable matched files from $SOURCE_DIR.
opendir DIR, $SOURCE_DIR
or die "Can't open $SOURCE_DIR: $!\n";
local @ARGV = grep { -f and !/^\./ and m/$PATTERN/ } readdir DIR
or die "No files to duplicate";
close DIR;
local $^I = "$DESTINATION_DIR$SEP*";
while (<>) {
print;
push @files_copied, $ARGV if eof;
}
Note that the above is not where the line-end conversion happens.
Anyway, I've thought of releasing this utility -- BBEdit/OS X workers
will find it especially handy, but it works in 'all' three realms --
but I've had some doubts about the separator issue. The little $SEP
routine above might not be 100% reliable. OTOH, my development style
tends to stress-test everything around me at certain stages, and this
has *always* worked.
I'd love to hear about hidden traps or alternatives.
1;
--
- Bruce
__bruce_van_allen__santa_cruz_ca__