use strict ; use warnings ; *SUE = \127 ; my $a = *SUE ; my $b = \*SUE ; printf "sex(\$a) = %10s and \${\*{\$a}{SCALAR}} = %d\n", sex($a), ${*{$a}{SCALAR}} ; printf "sex(\$b) = %10s and \${\*{\$b}{SCALAR}} = %d\n", sex($b), ${*{$b}{SCALAR}} ; sub sex { my ($thing) = @_ ; my $sex = ref($thing) ; return $sex ? "ref:$sex" : ref(\$thing) ; } ; #### sex($a) = GLOB and ${*{$a}{SCALAR}} = 127 sex($b) = ref:GLOB and ${*{$b}{SCALAR}} = 127 #### use strict ; use warnings ; use IO::Handle ; use FileHandle ; use Scalar::Util qw(reftype) ; print "1________________________________\n", 'open FH, ">&2" ;', "\n" ; open FH, ">&2" ; show('*FH', *FH) ; show('\*FH', \*FH) ; show('*FH{IO}', *FH{IO}) ; print "\n", "2________________________________\n", 'open my $OUT, ">&2" ; ', "\n" ; open my $OUT, ">&2" ; show('$OUT', $OUT) ; show('*$OUT{IO}', *$OUT{IO}) ; print "\n", "3________________________________\n", 'my $ih = new IO::Handle ;', "\n" ; my $ih = new IO::Handle ; print 'reftype($ih) = ', reftype($ih), "\n" ; print 'open $ih, ">&2" ;', "\n" ; open $ih, ">&2" ; show('$ih', $ih) ; show('*$ih{IO}', *$ih{IO}) ; print "\n", "4________________________________\n", 'my $fh = new FileHandle ;', "\n" ; my $fh = new FileHandle ; print 'reftype($fh) = ', reftype($fh), "\n" ; print 'open $fh, ">&2" ;', "\n" ; open $fh, ">&2" ; show('$fh', $fh) ; show('*$fh{IO}', *$fh{IO}) ; sub show { my ($tag, $FH) = @_ ; my $can = $FH->can('autoflush') ? "can 'autoflush'" : "can NOT 'autoflush'" ; my $tst = defined(-t $FH) ? "OK" : 'undef' ; printf $FH " show%-12s => %-18s fileno=%d test=%-5s %s\n", "($tag)", sex($FH), fileno($FH), $tst, $can ; } ; sub sex { my ($thing) = @_ ; my $sex = ref($thing) ; return $sex ? "ref:$sex" : ref(\$thing) ; } ; #### 1________________________________ open FH, ">&2" ; show(*FH) => GLOB fileno=4 test=OK can NOT 'autoflush' show(\*FH) => ref:GLOB fileno=4 test=OK can NOT 'autoflush' show(*FH{IO}) => ref:FileHandle fileno=4 test=undef can 'autoflush' 2________________________________ open my $OUT, ">&2" ; show($OUT) => ref:GLOB fileno=5 test=OK can NOT 'autoflush' show(*$OUT{IO}) => ref:FileHandle fileno=5 test=undef can 'autoflush' 3________________________________ my $ih = new IO::Handle ; reftype($ih) = GLOB open $ih, ">&2" ; show($ih) => ref:IO::Handle fileno=6 test=OK can 'autoflush' show(*$ih{IO}) => ref:FileHandle fileno=6 test=undef can 'autoflush' 4________________________________ my $fh = new FileHandle ; reftype($fh) = GLOB open $fh, ">&2" ; show($fh) => ref:FileHandle fileno=7 test=OK can 'autoflush' show(*$fh{IO}) => ref:FileHandle fileno=7 test=undef can 'autoflush' #### open my $FH, ..... ; #### use IO::Handle ; open my $FH = new IO::Handle, .... ; #### $STDERR = \*STDERR ; my $DATA = \*DATA ; #### $STDERR = bless \*STDERR, 'IO::Handle' ; my $DATA = bless \*DATA, 'IO::Handle' ; #### no strict ; announce(STDERR, "Hello World\n") ; sub announce { my $FH = shift ; print $FH @_ ; } ;