http://qs321.pair.com?node_id=721947


in reply to Re: How do I pretend a reference isn't a reference
in thread How do I pretend a reference isn't a reference

That's interesting - I've never used the tie interface before - I remember reading years ago that it is slow, and so just avoided it. Also, I thought that, because the variable is blessed into a package, it would still return it's class name when queried by ref. But you're right, it doesn't.

It is slower, especially tie'ing vs bless'ing, but FETCH'ing vs overload'ed stringification is only about 15% different:

#============================== package i18n::String; #============================== use strict; use warnings; no warnings 'once'; use overload q{""} => sub { $i18n::Current_Lang->maketext( ${ $_[0] } ) }; sub new { my $class = shift; my $string = shift; return bless( \$string, $class ); } #============================== package i18n::String2; #============================== use strict; use warnings FATAL => 'all', NONFATAL => 'redefine'; sub TIESCALAR { my $class = shift; my $string = shift; return bless( \$string, $class ); } sub FETCH { $i18n::Current_Lang->maketext( ${ $_[0] } ) } # BENCHMARK INIT ############## use Benchmark qw(cmpthese); cmpthese(1000000, { bless_string => sub { my $s = Burro::i18n::String->new('Januar +y'); }, tie_string => sub {tie my $s,Burro::i18n::String2,'January'} }); Rate tie_string bless_string tie_string 395257/s -- -30% bless_string 561798/s 42% -- # BENCHMARK FETCH ########### use Benchmark qw(cmpthese); my $s = i18n::String->new('January'); tie my $n,i18n::String2,'January'; cmpthese(1000000, { fetch_blessed => sub {"$s" }, fetch_tied => sub {"$n"} }); Rate fetch_tied fetch_blessed fetch_tied 168634/s -- -11% fetch_blessed 190476/s 13% --

That said, as far as I can see, tie works only on named variables:

my @months = map { tie my $var, i18n::String2, $_ } qw(January Febr +uary); print Dumper(\@months); $VAR1 = [ bless( do{\(my $o = 'January')}, 'i18n::String2' ), bless( do{\(my $o = 'February')}, 'i18n::String2' ) ];

So in the process of solving one issue, I could be introducing many more.

Defensive programming it is!

thanks for the suggestion BrowserUK

Clint