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

Re^2: How do I pretend a reference isn't a reference

by clinton (Priest)
on Nov 06, 2008 at 10:58 UTC ( #721947=note: print w/ replies, xml ) Need Help??


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


Comment on Re^2: How do I pretend a reference isn't a reference
Select or Download Code
Re^3: How do I pretend a reference isn't a reference
by BrowserUk (Pope) on Nov 06, 2008 at 11:16 UTC

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

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

    Um. The problem is that your map is returning the return value from tie, instead of the tied variable.

    If you change the map so:

    my @months = map{tie my $var, i18n::String2, $_; $var } qw(January +February);

    That problem goes away also. (I made a similar error myself earlier:)

    Provided that your _(...) sub does the right thing, your users need never be aware that they are dealing with anything other than simple scalars.

    So, trade your (one-time, up-front) care (and a little getting up to speed with the ins and outs of tie), as you develop your solution, for ongoing and continuous care by all your programmers?

    Your choice, but I know which way I would go :)


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      If you change the map so:
      my @months = map{tie my $var, i18n::String2, $_; $var } qw(Januar +y February);
      That problem goes away also. (I made a similar error myself earlier:)

      Actually, it doesn't :) What is stored in @months is not tie'd anonymous variables, but the result of FETCH'ing $var. In other words, it does the translation at compile time, rather than at runtime.

      Clint

        Well yes, but isn't your use/implementation of map in the example just a for testing?

        In your original example you had:

        my @days = ( _('Mon'),_('Tues'),_('Wed'),_('Thurs'),_('Fri'),_('Sat'),_('Sun') );

        So if your later example became

        my @days = map _( $_ ), qw[ Mon Tue Wed Thu Fri Sat Sun ];

        with all the choosing of packages and tieing hidden inside sub _{ ... }, then the translation will be done at runtime as required?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-10-01 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (21 votes), past polls