Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[perl #118105] filehandle promotion to IO::Handle not acting correctly

5 views
Skip to first unread message

James E Keenan via RT

unread,
May 21, 2013, 5:40:47 PM5/21/13
to perl5-...@perl.org
On Tue May 21 11:39:28 2013, ma...@cpan.org wrote:
> This is a bug report for perl from ma...@cpan.org,
> generated with the help of perlbug 1.39 running under perl 5.14.2.
>
>
> -----------------------------------------------------------------
>
> Filehandles should automatically be blessed into IO::Handle objects.

Why should they?

> Although this does happen, $fh->can does not report correctly.
>

It's not clear to me that such promotion happens in this case:


> Below is a simple test case to demonstrate this:
>
> $ perl -E 'open my $fh, "|-", "rev" or die $!; $fh->print("Hey");
> print "Can print" if $fh->can("print"); close $fh'
> yeH
> $
>

##########
$ cat 118105_fh.pl
#!/usr/env/perl
use strict;
use warnings;
use 5.10.1;
use Data::Dumper;
use Scalar::Util qw( reftype );
use Test::More qw( no_plan );

open my $fh, "|-", "rev" or die $!;
say STDERR "ref: ", ref($fh);
say STDERR "retypef: ", reftype($fh);
say STDERR Dumper $fh;
isa_ok($fh, "IO::Handle");
##########
Output:
##########
$ perl 118105_fh.pl
ref: GLOB
retypef: GLOB
$VAR1 = \*{'::$fh'};

not ok 1 - The object isa IO::Handle
# Failed test 'The object isa IO::Handle'
# at 118105_fh.pl line 13.
# The object isn't a 'IO::Handle' it's a 'GLOB'
1..1
# Looks like you failed 1 test of 1.
##########

Thank you very much.
Jim Keenan

---
via perlbug: queue: perl5 status: new
https://rt.perl.org:443/rt3/Ticket/Display.html?id=118105

Matthew Phillips

unread,
May 21, 2013, 6:17:32 PM5/21/13
to perlbug-...@perl.org, perl5-...@perl.org
I'm not entirely clear on the perl internals that involve open() dealing with FileHandle/IO::Handle, but if you use the debugger to step through the oneliner I provided (or add a $fh->print('foo') to your provided test script), you will see that IO::Handle is in the handles @ISA, and IO::Handle::print() is run. This implies that $fh->can('print') should return the sub to that method, not undef.

Cheers,
Matt

Leon Timmermans via RT

unread,
May 21, 2013, 6:21:02 PM5/21/13
to perl5-...@perl.org
On Tue May 21 11:39:28 2013, ma...@cpan.org wrote:
> Filehandles should automatically be blessed into IO::Handle objects.
> Although this does happen, $fh->can does not report correctly.

Unblessed Globs aren't quite automatically blessed into IO::Handle,
they're special cased in interesting ways that include autoloading
IO::Handle and pretending they are IO::Handle's. This special case seems
to leak.

I'm wondering if they shouldn't be auto-blessed instead of the current
weirdness.

> Below is a simple test case to demonstrate this:
>
> $ perl -E 'open my $fh, "|-", "rev" or die $!; $fh->print("Hey");
> print "Can print" if $fh->can("print"); close $fh'
> yeH

It seems isa has similar issues.

Leon

---
via perlbug: queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=118105

mattp @ cpan . org

unread,
May 21, 2013, 2:39:28 PM5/21/13
to bugs-bi...@rt.perl.org
# New Ticket Created by ma...@cpan.org
# Please include the string: [perl #118105]
# in the subject line of all future correspondence about this issue.
# <URL: https://rt.perl.org:443/rt3/Ticket/Display.html?id=118105 >


This is a bug report for perl from ma...@cpan.org,
generated with the help of perlbug 1.39 running under perl 5.14.2.


-----------------------------------------------------------------

Filehandles should automatically be blessed into IO::Handle objects.
Although this does happen, $fh->can does not report correctly.

Below is a simple test case to demonstrate this:

$ perl -E 'open my $fh, "|-", "rev" or die $!; $fh->print("Hey"); print "Can print" if $fh->can("print"); close $fh'
yeH
$

-----------------------------------------------------------------
---
Flags:
category=core
severity=low
---
Site configuration information for perl 5.14.2:

Configured by root at Mon Aug 6 08:08:01 EDT 2012.

Summary of my perl5 (revision 5 version 14 subversion 2) configuration:

Platform:
osname=freebsd, osvers=9.0-release, archname=amd64-freebsd-thread-multi
uname='freebsd double 9.0-release freebsd 9.0-release #0: tue jan 3 07:46:30 utc 2012 ro...@farrell.cse.buffalo.edu:usrobjusrsrcsysgeneric amd64 '
config_args='-sde -Dprefix=/usr/local -Darchlib=/usr/local/lib/perl5/5.14.2/mach -Dprivlib=/usr/local/lib/perl5/5.14.2 -Dman3dir=/usr/local/lib/perl5/5.14.2/perl/man/man3 -Dman1dir=/usr/local/man/man1 -Dsitearch=/usr/local/lib/perl5/site_perl/5.14.2/mach -Dsitelib=/usr/local/lib/perl5/site_perl/5.14.2 -Dscriptdir=/usr/local/bin -Dsiteman3dir=/usr/local/lib/perl5/5.14.2/man/man3 -Dsiteman1dir=/usr/local/man/man1 -Ui_malloc -Ui_iconv -Uinstallusrbinperl -Dcc=cc -Duseshrplib -Dinc_version_list=none -Dccflags=-DAPPLLIB_EXP="/usr/local/lib/perl5/5.14.2/BSDPAN" -Doptimize=-O2 -pipe -fno-strict-aliasing -Ui_gdbm -Dusethreads=y -Dusemymalloc=n -Duse64bitint -Dusemultiplicity=y'
hint=recommended, useposix=true, d_sigaction=define
useithreads=define, usemultiplicity=define
useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
use64bitint=define, use64bitall=define, uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='cc', ccflags ='-DAPPLLIB_EXP="/usr/local/lib/perl5/5.14.2/BSDPAN" -DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include',
optimize='-O2 -pipe -fno-strict-aliasing',
cppflags='-DAPPLLIB_EXP="/usr/local/lib/perl5/5.14.2/BSDPAN" -DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'
ccversion='', gccversion='4.2.1 20070831 patched [FreeBSD]', gccosandvers=''
intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=8, prototype=define
Linker and Libraries:
ld='cc', ldflags ='-pthread -Wl,-E -fstack-protector -L/usr/local/lib'
libpth=/usr/lib /usr/local/lib
libs=-lm -lcrypt -lutil
perllibs=-lm -lcrypt -lutil
libc=, so=so, useshrplib=true, libperl=libperl.so
gnulibc_version=''
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' -Wl,-R/usr/local/lib/perl5/5.14.2/mach/CORE'
cccdlflags='-DPIC -fPIC', lddlflags='-shared -L/usr/local/lib -fstack-protector'

Locally applied patches:


---
@INC for perl 5.14.2:
/usr/local/lib/perl5/5.14.2/BSDPAN
/usr/local/lib/perl5/site_perl/5.14.2/mach
/usr/local/lib/perl5/site_perl/5.14.2
/usr/local/lib/perl5/5.14.2/mach
/usr/local/lib/perl5/5.14.2
.

---
Environment for perl 5.14.2:
HOME=/home/mmp
LANG (unset)
LANGUAGE (unset)
LD_LIBRARY_PATH (unset)
LOGDIR (unset)
PATH=/home/mmp/.rbenv/bin:/home/mmp/prv/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/home/mmp/bin
PERL_BADLANG (unset)
SHELL=/usr/local/bin/bash

Father Chrysostomos via RT

unread,
Jul 7, 2013, 1:11:45 AM7/7/13
to perl5-...@perl.org
On Tue May 21 15:21:02 2013, LeonT wrote:
> On Tue May 21 11:39:28 2013, ma...@cpan.org wrote:
> > Filehandles should automatically be blessed into IO::Handle objects.
> > Although this does happen, $fh->can does not report correctly.
>
> Unblessed Globs aren't quite automatically blessed into IO::Handle,
> they're special cased in interesting ways that include autoloading
> IO::Handle and pretending they are IO::Handle's. This special case seems
> to leak.
>
> I'm wondering if they shouldn't be auto-blessed instead of the current
> weirdness.

But then you would have to bless *foo when assigning *foo = *$fh{IO}.
Since that is obviously not going to work very well (after all,
blessings only apply to refs and *foo->print involves none), we will
*still* have to support the old way, so the autoblessing would
accomplish nothing.

The bug is in UNIVERSAL::can. This is actually a duplicate of #113932.
(I don’t think it should be merged, as that ticket has other issues, too.)

>
> > Below is a simple test case to demonstrate this:
> >
> > $ perl -E 'open my $fh, "|-", "rev" or die $!; $fh->print("Hey");
> > print "Can print" if $fh->can("print"); close $fh'
> > yeH
>
> It seems isa has similar issues.
>
> Leon

--

Father Chrysostomos

Father Chrysostomos via RT

unread,
Jul 8, 2013, 5:03:58 PM7/8/13
to perl5-...@perl.org
On Tue May 21 15:21:02 2013, LeonT wrote:
> It seems isa has similar issues.

Should ARGV->isa("IO::Handle") return true? I’m not so sure. I don’t
really know the answer.

If we make UNIVERSAL::isa treat \*ARGV as *ARGV{IO} when calling
sv_derived_from, this will stop working:

$ ./perl -lIlib -e 'print *ARGV->isa("GLOB")'
1

--

Father Chrysostomos

Father Chrysostomos via RT

unread,
Jul 8, 2013, 9:07:55 PM7/8/13
to perl5-...@perl.org
On Mon Jul 08 14:03:57 2013, sprout wrote:
> On Tue May 21 15:21:02 2013, LeonT wrote:
> > It seems isa has similar issues.
>
> Should ARGV->isa("IO::Handle") return true? I’m not so sure. I don’t
> really know the answer.
>
> If we make UNIVERSAL::isa treat \*ARGV as *ARGV{IO} when calling
> sv_derived_from, this will stop working:
>
> $ ./perl -lIlib -e 'print *ARGV->isa("GLOB")'
> 1

I don’t think we should touch isa. I have fixed the can bug in 4178f891.
0 new messages