Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Odd error message on open()-ing scalar ref

Reply
Thread Tools

Odd error message on open()-ing scalar ref

 
 
Hartmut Camphausen
Guest
Posts: n/a
 
      07-13-2008
Hi all,

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
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      07-13-2008
Hartmut Camphausen <> wrote:
> Hi all,
>
> Consider this snippet:
>
> open ($fh, '<', $fref) || die $!
> warn "(1) $!" if $!;
>
>
> It produces this output:
>
> (1) Bad file descriptor at E:\[Public]\cgi-bin\lib\Text\testmisc.pl
> line 10.


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.
 
Reply With Quote
 
 
 
 
Michael Carman
Guest
Posts: n/a
 
      07-13-2008
Hartmut Camphausen wrote:
> open ($fh, '<', $fref) || die $!
> warn "(1) $!" if $!;
>
> [...]
>
> 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.


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
 
Reply With Quote
 
Hartmut Camphausen
Guest
Posts: n/a
 
      07-13-2008
Hi xho, Michael,

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


--
------------------------------------------------
Hartmut Camphausen h.camp[bei]textix[punkt]de
 
Reply With Quote
 
Leon Timmermans
Guest
Posts: n/a
 
      07-13-2008
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 $!. Please
post correct code on the newsgroup

Leon Timmermans
 
Reply With Quote
 
Hartmut Camphausen
Guest
Posts: n/a
 
      07-14-2008
Hi Leo,

> >
> > 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


--
------------------------------------------------
Hartmut Camphausen h.camp[bei]textix[punkt]de
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-14-2008
Leon Timmermans <> wrote in newsan.2008.07.13.23.19.47
@gmail.com:

> 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 <>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make a blessable anonymous scalar ref? J Krugman Perl Misc 17 03-15-2005 10:15 PM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
Expanding scalar hash element in array ref. Dmitri Zakharov Perl 2 06-11-2004 11:11 AM
Expanding scalar hash element in array ref. Dmitri Zakharov Perl Misc 5 06-11-2004 11:11 AM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57