Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Axel Rose (axelrose)
Assigned to: Nobody/Anonymous (nobody)
Summary: IO::File + leading space for hard disk
Initial Comment:
The following failes:
use IO::File;
my $file = ' myharddisk:folder:filename';
my $fh = IO::File->new;
$fh->open( '$file', "r" ) or die "unable to open'$file' - $!";
Note the leading space in the hard disk name " myharddisk".
Axel
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=107940&aid=664987&group_id=7940
>The following failes:
>$fh->open( '$file', "r" ) or die "unable to open'$file' - $!";
>
>Note the leading space in the hard disk name " myharddisk".
Also note the single quotes around $file in the method call. Could that
be it, or is this a case of badly typed over code?
--
Bart.
I noted in the bug report that IO::File->open uses either sysopen for
numeric modes, or two-arg open. There is no way, that I know of, to open a
path that has leading space, period, with two-arg open. If the path is
relative, you can protect it with "./", but that is not possible here.
I -- or someone who wants to -- needs to patch IO::File to use three-arg
open. It'd be pretty easy, but will require testing, of course. Something
like (untested)
if (! File::Spec->file_name_is_absolute($file)) {
$file = File::Spec->catfile(File::Spec->curdir(),$file);
}
- $file = IO::Handle::_open_mode_string($mode) . " $file\0";
+ return open($fh, IO::Handle::_open_mode_string($mode), $file);
}
open($fh, $file);
Would just need to make sure this doesn't break anything (including
pre-perl 5.6, if IO still works there).
--
Chris Nandor pu...@pobox.com http://pudge.net/
Open Source Development Network pu...@osdn.com http://osdn.com/
>Um 11:13 Uhr -0500 09.01.2003, schrieb Chris Nandor:
>> I -- or someone who wants to -- needs to patch IO::File ...
>
>I'd be willing but fear the consequences for other platforms
>or would it just be a MacPerl only patch?
A consequence would be that it would only work for 5.6.0 and up, because
I think that's when the three argument open() was introduced. That would
imply that future bugfixes on IO::File would be wasted on 5.005_03,
which still is a very popular Perl version -- which replace something
that isn't really broken?
The safer but more complex way, is to always use sysopen(), and thus,
convert the modes into numbers using the constants from Fctnl. I could
have some errors, but I think this about covers it:
use Fcntl;
my %mode = (
'<' => O_RDONLY,
'+<' => O_RDWR,
'>' => O_WRONLY | O_TRUNC | O_CREAT,
'+>' => O_RDWR | O_TRUNC | O_CREAT,
'>>' => O_WRONLY | O_APPEND | O_CREAT,
'+>>' => O_RDWR | O_APPEND | O_CREAT,
);
You get the numerical mode from the string representation *after*
IO::Handle::_open_mode_string() has had its hand in normalising it.
Besides... the PERMS you pass to this function, only has effect if your
provide a numerical mode. Even though it's documented... Surely that
can't be right?
The patched open() as I would propose it (warning: untested):
my %mode = (
'<' => O_RDONLY,
'+<' => O_RDWR,
'>' => O_WRONLY | O_TRUNC | O_CREAT,
'+>' => O_RDWR | O_TRUNC | O_CREAT,
'>>' => O_WRONLY | O_APPEND | O_CREAT,
'+>>' => O_RDWR | O_APPEND | O_CREAT,
);
sub open {
@_ >= 2 && @_ <= 4
or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])';
my ($fh, $file) = @_;
if (@_ > 2) {
my ($mode, $perms) = @_[2, 3];
$mode =~ /^\d+$/
or $mode = $mode{IO::Handle::_open_mode_string($mode)};
defined $perms or $perms = 0666;
return sysopen($fh, $file, $mode, $perms);
}
open($fh, $file);
}
--
Bart.
Note: this could only work for when IO::File has been passed more than 1
parameter (because the user might want magic open). Your patch takes care
of that, but I just wanted to state it explicitly.
Another problem is that sometimes sysopen() is buggy. :-) There are a few
open bugs in MacPerl for this. Or maybe one. I forget. It gets used
less, and because of that has more bugs, I think.
Anyway, we could also try if ($] >= 5.006) ... but I think that perl still
might fail on the prototyping. Yeah, it does.
I'll probably whip up more than one patch, including yours, and let them
(p5p or Graham) decide which to use.