Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How do I pretend a reference isn't a reference

by clinton (Priest)
on Nov 06, 2008 at 02:42 UTC ( #721884=perlquestion: print w/ replies, xml ) Need Help??
clinton has asked for the wisdom of the Perl Monks concerning the following question:

I'm implementing an i18n framework for my webapp, and trying to make it as transparent as possible: you don't need to know that a variable contains a string that will be magically translated into your chosen language. It just works, eg:

my @days = _('Mon'),_('Tues'),_('Wed'),_('Thurs'),_('Fri'),_('S +at'),_('Sun'); sub day_of_week { my $self = shift; return $days[ $self->{day_num} ]; }

The idea is that any string marked with _( ) gets translated automatically. But there are two "phases": compilation and runtime. At runtime, I map *::_ to a sub which does the translation, but at compile time, I map *::_ to a sub which blesses the string into a simple overloaded class, that looks like this:

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 ); } 1;

This class delays the translation of the string until the moment that the string needs to be used / stringified, at which point the overloaded stringification calls maketext on the original string.

The problem:

For the most part, this is transparent. I don't need to care whether variables contain strings-to-be-translated or just ordinary strings. The one place where this falls down is ref.

I know that we SHOULD care about the actual return value of ref, but how often do we just leave that out of our code:

sub good { my %params = ref $_[0] eq 'HASH? ? %{shift @_} : @_; } sub bad_but_typical { my %params = ref $_[0] ? %{shift @_} : @_; }

The only thing I could come up with to make this work is this:

{ no strict 'refs'; my $zero_isa = '0::ISA'; # Use a symbolic reference to + make package '0' *$zero_isa = ['i18n::String']; # inherit from i18n::String } 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,'0' ); # bless the scalar ref into +class '0' instead of $class } 1;

That works, because calling ref $string_object returns zero, which is false. But it's one of those things you hope never to see in production code. And of course, once you've used the "0" class for one object type, you can't use it for any others (unless you were to store the original class name as a property of the object, and let class '0' redispatch it to the correct class.)


So is the answer to this just "teach your fellow developers to program more defensively", or is there another way around it?

Comment on How do I pretend a reference isn't a reference
Select or Download Code
Re: How do I pretend a reference isn't a reference
by GrandFather (Cardinal) on Nov 06, 2008 at 02:55 UTC

    "teach your fellow developers to program more defensively" is good in any case.


    Perl reduces RSI - it saves typing
Re: How do I pretend a reference isn't a reference
by ikegami (Pope) on Nov 06, 2008 at 02:57 UTC

    The real problem is that you are trying to get a single function to accept two different styles of arguments. As you see, that can get you in trouble.

    If you want to change the check, eval { %{$_[0]} } works best to detect a hash.

    sub better { my %params = eval { %{$_[0]} } || @_; }

    But in this case, the following should do fine:

    sub better { my %params = @_ == 1 ? %{$_[0]} : @_; }

    Of course, you could always give the sub what it wants:

    bad_but_typical("$i18n"); # Force stringification

    Update: Added missing curlies.

Re: How do I pretend a reference isn't a reference
by Anonymous Monk on Nov 06, 2008 at 03:00 UTC
    At runtime, I map *::_ to a sub which does the translation, but at compile time, I map *::_ to a sub which blesses the string into a simple overloaded class
    How is that possible? Sure you can redefine _, but that won't do any blessing at compile time.
      How is that possible? Sure you can redefine _, but that won't do any blessing at compile time.

      Quite possible. My webapp is large, and runs within mod_perl, so I have an initialisation process which loads most required modules at the beginning. In my i18n class, which gets loaded as early as possible, I have this code:

      { no warnings 'redefine'; sub compile_mode { *::_ = sub { i18n::String->new(@_) }; } sub run_mode { *::_ = sub { $i18n::Current_Lang->maketext(@_) }; } } BEGIN { compile_mode(); }

      Once initialisation is finished, my loader calls i18n::run_mode. If I need to require any large modules that belong to my app during runtime, instead of using the require builtin, I call i18n::i18n_require('Module::Name'):

      sub i18n_require { my $original = my $path = shift; i18n::compile_mode(); $path=~s{::}{/}g; my $error; eval {require $path.'.pm'} or $error = $@ || 'Unknown error'; i18n::run_mode(); die( sprintf( "Error loading '%s' at %s line %d:\n%s\n", $original, (caller)[ 1, 2 ], $error ) ) if $error; return $INC{$path}; }

      I tried overriding UNIVERSAL::require, but certain modules outside my control didn't appreciate that. Anyway, doing it this way is explicit, and only a few characters more.

        I was thinking
        use yourblah; my $foo = _('this is blahblah');
        at compile time, $foo doesn't get blessed.
Re: How do I pretend a reference isn't a reference
by sflitman (Hermit) on Nov 06, 2008 at 04:24 UTC
    I wonder if you'd be better off using a source translator like Damian Conway's Filter::Simple. Then you could use more natural Perlish syntax, and just replace any "..." literal with the translation based on your module recognizing the literal text and replacing it from your hash for the current language (maybe stored in an environment variable). Then you wouldn't have runtime issues at all, all the translating would be at compile time...but you could have a runtime interface to your module for on-demand translation also, of course.

    HTH,
    SSF

      That wouldn't help for a few reasons:

      • I don't want to translate all strings, so strings to be translated should be explicitly labelled.
      • This is a webapp, so the current language could change for each request, so strings shouldn't be translated at compile time
      • The app is 60,000 lines of code, plus CPAN modules - running a filter over that would be slow and error prone.

      clint

Re: How do I pretend a reference isn't a reference
by BrowserUk (Pope) on Nov 06, 2008 at 08:04 UTC

    Update: Actually this would work.

    If you had your function return tied scalars instead of blessed references, this problem would go away. This is one of those places where the tie mechanism just seems to make more sense.


    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.

      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

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://721884]
Approved by GrandFather
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2014-08-20 19:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (122 votes), past polls