Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Spotting an empty array as argument (updated: select tied handle)

by LanX (Saint)
on Mar 27, 2021 at 00:28 UTC ( [id://11130412]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: Spotting an empty array as argument (updated: select tied handle)
by haukex (Archbishop) on Mar 27, 2021 at 08:55 UTC
    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 :-) ).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130412]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-03-28 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found