Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by clinton (Priest)
on Nov 06, 2008 at 10:04 UTC ( #721933=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

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.


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 Anonymous Monk on Nov 06, 2008 at 12:48 UTC
    I was thinking
    use yourblah; my $foo = _('this is blahblah');
    at compile time, $foo doesn't get blessed.
        No it doesnt.
        sub _ { 'compile time '.@_}; local *_ = sub { 'run time '.@_}; # the above is what your module/init code does my $c = _(1); die $c; __END__ run time 1 at - line 6.
        You were saying that $c is one thing at runtime, another at compile time. Its only something at runtime.

        That's not what my code does, nor was I saying that $c was one thing at compile time and one at run time. This is what I do:

        # init the app sub _ { 'compile time '.@_}; my $compile = _(1); sub foo { _(1) }; # finished compiling, so redefine sub _ sub _ { 'run time '.@_}; # run the code my $run = foo(); print " $compile - $run\n" __END__ compile time 1 - run time 1
        UPDATE: Meant to reply to Re^5: How do I pretend a reference isn't a reference

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2014-12-29 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls