Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re^2: How do I pretend a reference isn't a reference by clinton
in thread How do I pretend a reference isn't a reference by clinton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-26 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found