package Logndebug;
use strict;
use warnings;
use Exporter;
our @ISA = qw[ Exporter ];
our @EXPORT = qw[ LOG DEBUG logging ];
sub LOG {
return unless fileno( *STDLOG );
print STDLOG @_;
}
sub DEBUG {
return unless fileno( *STDDBG );
print STDDBG @_;
}
sub logging {
my( $logging, $debugging ) = @_;
*STDLOG = *{ $logging } if defined $logging and fileno( $logging );
*STDDBG = *{ $debugging } if defined $debugging and fileno( $debugging );
}
1;
####
package SillyVec3D;
use strict;
use warnings;
use Logndebug;
sub new {
my $class = shift;
my $self = bless { @_ }, $class;
for my $attr ( keys %{ $self } ) {
no strict 'refs';
eval qq[
*{ $attr } = sub {
LOG( "$attr called with [\@_]" );
my \$self = shift;
DEBUG( "$attr set to: ", \$self->{ $attr } = shift ) if \@_;
\$self->{ $attr };
}
];
}
return $self;
}
1;
####
#! perl -slw
use strict;
use SillyVec3D;
our( $LOG, $DBG );
SillyVec3D::logging(
$LOG ? *STDERR : undef,
$DBG ? *STDERR : undef,
);
my $o = SillyVec3D->new(
X => 1, Y => 2, Z => 3
);
print $o->X, $o->Y, $o->Z;
$o->X( 4 );
$o->Y( 5 );
$o->Z( 6 );
print $o->X, $o->Y, $o->Z;
####
C:\test>t-SillyVec3D
123
456
C:\test>t-SillyVec3D -LOG
X called with [SillyVec3D=HASH(0x32cf38)]
Y called with [SillyVec3D=HASH(0x32cf38)]
Z called with [SillyVec3D=HASH(0x32cf38)]
123
X called with [SillyVec3D=HASH(0x32cf38) 4]
Y called with [SillyVec3D=HASH(0x32cf38) 5]
Z called with [SillyVec3D=HASH(0x32cf38) 6]
X called with [SillyVec3D=HASH(0x32cf38)]
Y called with [SillyVec3D=HASH(0x32cf38)]
Z called with [SillyVec3D=HASH(0x32cf38)]
456
C:\test>t-SillyVec3D -DBG
123
X set to: 4
Y set to: 5
Z set to: 6
456
C:\test>t-SillyVec3D -LOG -DBG
X called with [SillyVec3D=HASH(0x8cf48)]
Y called with [SillyVec3D=HASH(0x8cf48)]
Z called with [SillyVec3D=HASH(0x8cf48)]
123
X called with [SillyVec3D=HASH(0x8cf48) 4]
X set to: 4
Y called with [SillyVec3D=HASH(0x8cf48) 5]
Y set to: 5
Z called with [SillyVec3D=HASH(0x8cf48) 6]
Z set to: 6
X called with [SillyVec3D=HASH(0x8cf48)]
Y called with [SillyVec3D=HASH(0x8cf48)]
Z called with [SillyVec3D=HASH(0x8cf48)]
456