in reply to Spotting an empty array as argument

Another idea:

Since one can change the output handle with select , it should also be possible to change the print method of the IO object.

In a perfect world are print and say only calling that method.

But who knows what kind of magic rules here and I'm too tired to check this out now. :)

update

See also tied file handles:

A class implementing a filehandle should have the following methods: TIEHANDLE classname, LIST READ this, scalar, length, offset READLINE this GETC this WRITE this, scalar, length, offset PRINT this, LIST <---- PRINTF this, format, LIST BINMODE this EOF this FILENO this SEEK this, position, whence TELL this OPEN this, mode, LIST CLOSE this DESTROY this UNTIE this

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

Replies are listed 'Best First'.
Re^2: Spotting an empty array as argument
by haukex (Archbishop) on Mar 26, 2021 at 22:16 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 :-) ).