You're complaining that a poorly-known, but well-documented, feature of open() doesn't throw a warning when you accidentally trip over it. Ok ... I can buy that.Well, not really, in fact I wrote:
As a side note, I wonder wether perl could emit a warning when one tries to open() an anonymous file for reading (or for writing) only...and if you look at the emphasized text you'll notice that I meant to ask for some user consideration about this issue, rather than claiming that a warning should be there and then complaining for there not being. It's a meditation after all... but the only one who addressed directly that cmt (an anonymous monk) completely missed my point!
I think the real discussion needs to be whether or not there should be a warning when opening an anonymous filehandle. That's a fair discussion to have.Personally I do not think that there should be, and that is not what I meant.
The keyword (well, "keyphrase") in my original cmt is not "anonymous filehandle", but "for reading only". In fact I do think that that anonymous filehandle feature of open() is great, and as you wrote, it's well documented. OTOH the documents literally say:
As a special case the 3 arg form with a read/write mode and the third argument being "undef": open(TMP, "+>", undef) or die ... opens a filehandle to an anonymous temporary file. Also using "+<" works for symmetry, but you really should consider writing something to the temporary file first. You will need to seek() to do the reading.Notice that it doesn't even mention that anonymous filehandles can be opened in modes other than '+>' and '+<'.
Now, follow me: why should one want to open an anonymous (temporary) filehandle? To store something in it and then to retrieve it. But if you open it '<', then you'll get an empty anonymous file to read from. What can you do with it? Apart from interacting with the filesystem, appearently nothing!! Now, this is not strictly true, as this example code shows:
But you'll agree that it's rather awkward. It could make more sense to open it in write-only mode and then re-open it in read mode later when needed to retrieve what's been stored into it...#!/usr/bin/perl use strict; use warnings; open my $fh, '<', undef or die $!; open my $fh2, '>&=', $fh or die $!; select $fh2; $|++; print "foo\n"; seek $fh, 0, 0; print STDOUT <$fh>; __END__
Unfortunately, you're probably going to be Warnocked if you post to p5p about this. They tend to frown upon making changes that violate the Principle of Least Surprise.Yep, I think you're right. But then again this was just a meditation...
In reply to Re^4: I've been bit in the neck by open()
by blazar
in thread I've been bit in the neck by open()
by blazar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |