in reply to Re: Spotting an empty array as argument
in thread Spotting an empty array as argument

tieing filehandles is tricky business, as I learned when I wrote Tie::Handle::Base... but now there's Tie::Handle::Base to make tied filehandles easier ;-)

Replies are listed 'Best First'.
Re^3: Spotting an empty array as argument (updated: select tied handle)
by LanX (Saint) on Mar 27, 2021 at 00:28 UTC
    Awesome, an expert! =)

    So, is it feasible?

    update

    Looks good to me, even with core modules. =)

    use v5.12; # enable say & strict use warnings; { package MySay; use Data::Dump qw/pp dd/; use Carp; require Tie::Handle; our @ISA = qw(Tie::Handle); sub TIEHANDLE { #carp pp '\@_: ', \@_; bless \ my $i, shift } sub PRINT { my $self = shift; print STDOUT "$_" for @_ } sub new { my $self = shift; open my $fh, ">&STDOUT"; tie *$fh, 'MySay'; return($fh); } # sub DESTROY { # print STDOUT "DESTROY"; # # my $self = shift; # select(STDOUT); # } } { package main; # ---- test vars $_='$_'; my @a = map { "\$a[$_]" } 0..2; my @b; { select MySay->new(); say; say @b; say @a; select(STDOUT) } say "AF","TER" }
    C:/Strawberry/perl/bin\perl.exe -w d:/tmp/pm/tie_handle.pl $_ $a[0] $a[1] $a[2] AFTER

    with some effort it might even be possible to automatically destroy the selection at end of scope, such that one only needs to write MySay->select() once without any further need for visible select

    But I got tired and wanted to get it done. :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Looks good to me, even with core modules. =)

      Yes, Tie::Handle and Tie::StdHandle aren't too bad, they're just not necessarily consistent across Perl versions. For example, Tie::StdHandle::BINMODE didn't handle a "layer" argument until I patched it (this fix wasn't included until v5.32.0). Also, its sub WRITE still doesn't return the length of the data written, like syswrite normally would. Tie::Handle::Base works the same all the way down to Perl 5.8.1 (of course I have lots of tests :-) ).