Consider this snippet:
#!perl.exe -w
use strict;
my $f = 'abc';
my $fref = \$f;
my $fh;
warn "(0) $!" if $!;
open ($fh, '<', $fref) || die $!
warn "(1) $!" if $!;
print "\n", <$fh>;
seek ($fh, 1, 0) || die $!;
print "\n", <$fh>;
It produces this output:
(1) Bad file descriptor at E:\[Public]\cgi-bin\lib\Text\testmisc.pl
line 10.
abc
bc
Tool completed successfully
Any idea, why open() generates an error ($! set) without failing?
open() dies correctly if I dereference $fref via $$fref (No such
file...).
I'm running Perl v5.8.8.
Clueless,
H.
--
--
------------------------------------------------
Hartmut Camphausen h.camp[bei]textix[punkt]de
Open did not fail. Therefore, the value of $! is meaningless. Meaningless
can be anything, including the value you see above.
> Any idea, why open() generates an error ($! set) without failing?
Setting $! is *not* generating an error[1]. The way that open indicates an
error is by returning a false value (or conceivably by dying). It did not
do that.
[1] And "open" is not setting it anyways, at least not directly. Most
likely your use of open is triggering the loading of PerlIO::scalar, and it
is the loading of PerlIO::scalar that is setting $!, but in an innocuous
manner. On my system, if I preload that module by "use PerlIO::scalar",
and then reset $! to 0, then do your open, after the open $! remains 0.
But that doesn't really matter, as by inspecting $! when an error has not
occurred you are living in sin.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
The open() entry in perlfunc contains part of the answer:
Since v5.8.0, [...] you can open file handles to "in memory"
files held in Perl scalars via:
open($fh, '>', \$variable) || ..
This is what you're doing except that you're opening the handle for
reading instead of writing. The open() isn't returning false because it
isn't failing. This can be seen in the output of your print() statements.
The rest of the answer is in the entry for $! in perlvar:
if a system or library call fails, it sets this variable.
This means that the value of $! is meaningful only *immediately*
after a failure:
Since there was no failure, the value of $! isn't meaningful.
-mjc
many thanks for your quick and enlightening responses.
@ xho:
> Open did not fail. Therefore, the value of $! is meaningless. Meaningless
> can be anything, including the value you see above.
> [...]
> [1] And "open" is not setting it anyways, at least not directly.
Ah yes! I kind of thought that function failing and setting $! belongs
together - and /vice versa/: No fail, no $!.
But as Michael quoted:
> The rest of the answer is in the entry for $! in perlvar:
>
> if a system or library call fails, it sets this variable.
> This means that the value of $! is meaningful only *immediately*
> after a failure:
I should better have remembered that, just read it a short while ago :-p
Thanks again + mfg,
Hartmut
> Hi all,
>
> Consider this snippet:
>
> open ($fh, '<', $fref) || die $!
> warn "(1) $!" if $!;
>
You have a syntax error here, you forgot the semicolon after $!. Please
post correct code on the newsgroup ;-)
Leon Timmermans
> >
> > open ($fh, '<', $fref) || die $!
> > warn "(1) $!" if $!;
> >
>
> You have a syntax error here, you forgot the semicolon after $!.
Oh yes. You are right. I should have taken it with me, just like the
other one.
Thanks for the hint anyway :-)
mfg, Hartmut
> On Sun, 13 Jul 2008 22:51:10 +0200, Hartmut Camphausen wrote:
>
>> Hi all,
>>
>> Consider this snippet:
>>
>> open ($fh, '<', $fref) || die $!
>> warn "(1) $!" if $!;
>>
>
> You have a syntax error here, you forgot the semicolon after $!.
That might have been more obvious had the OP not decided to format Perl
as if it is assembly language.
I realize beauty is in the eye of the beholder but I would urge the OP
to read
perldoc perlstyle
to ensure that his code is accessible to as many people as possible
(especially when requesting help in a public forum).
> Please post correct code on the newsgroup ;-)
That would help.
Sinan
--
A. Sinan Unur <1u...@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/