Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

select and IO::Handle

by John M. Dlugosz (Monsignor)
on Aug 04, 2001 at 00:27 UTC ( #102146=perlquestion: print w/replies, xml ) Need Help??
John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I was suprised to find the following
my $string; my $SH= new IO::Scalar; $SH->open (\$string); select ($SH); ## here
choked, complaing that $SH was not a reference to a glob.

Then I also found that $SH->select isn't a member, either.

Why doesn't this work? I thought IO::Handle objects were glob refs, which is why you can use print {$handle} "hello world".

Looking at, it seems that it's a blessed hash. It doesn't inherit from IO::Handle, even though the docs say "This class implements objects which behave just like FileHandle (or IO::Handle) objects..." is that incorrect?

Just how do you select one of these?


Replies are listed 'Best First'.
Re: select and IO::Handle
by danger (Priest) on Aug 04, 2001 at 01:21 UTC

    I found that using either of the tied interfaces worked as you expect (both documented in the POD's for Perl-5.004 or later):

    #!/usr/bin/perl -w use strict; use IO::Scalar; my $string; tie *OUT, 'IO::Scalar', \$string; select OUT; print "line 1\nline 2\n", "line 3\n"; select STDOUT; print "The string contains:\n$string\n"; ### OR this way: $string = ''; my $SH = IO::Scalar->new_tie(\$string); $SH->print("line 1\n"); # use the OO method print $SH "line 2\n"; # use it like a filehandle select $SH; # select it as default output handle print "line 3\n"; select STDOUT; print "The string contains:\n$string" __END__
Re: select and IO::Handle
by runrig (Abbot) on Aug 04, 2001 at 01:30 UTC
    IO::Scalar does not return an IO::Handle, it returns a blessed hash reference. The author said he tried to subclass IO::Handle, but ran into 'xxx on unopened filehandle' warnings. You have to use methods on an IO::Scalar object, unless you are using the tied interface, but then you would be using a tied filehandle/glob.

    I'm actually working on a patch for IO::Scalar so that it will return a blessed glob reference with a tied glob so that it will behave more like a real filehandle, and things that return IO::Scalar objects can be passed to things which expect filehandles. If there's a better fix, I'd be glad to hear of it :-)

    Update: IO::Scalar now returns a blessed glob reference and so the returned object behaves more like a real filehandle. However, modern perls let you open a filehandle directly to a scalar, which makes these sorts of modules obsolete.

      I was just talking to the author, too.

      A better fix: Only implement actual methods for the TIE. Also have a function to create one and return an IO::Handle to it as one call.

      While you're at it, note that WRITE method is missing.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://102146]
Approved by root
[marioroy]: Is List::Util::XS a core Perl module?
[stevieb]: corelist List::Util::XS says "List::Util::XS was first released with perl v5.10.1"
[stevieb]: so yes, since v5.10.1 :)
[MidLifeXis]: According to [metaperl:// corelist], Perl 5.10.1
Lady_Aleena blurbles. One of her favorite modiles is now in "maintenance mode" in favor of a new fangled module.
[MidLifeXis]: D'oh. Slow out of the gate.
[marioroy]: Thanks stevieb, MidLifeXis.

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2017-05-24 17:52 GMT
Find Nodes?
    Voting Booth?