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

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

by BrowserUk (Patriarch)
on Nov 06, 2008 at 11:16 UTC ( [id://721955]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^4: How do I pretend a reference isn't a reference
by clinton (Priest) on Nov 06, 2008 at 11:32 UTC
    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.
        Well, my original example was defining the array @days at compile time, because that's going to be more efficient than:
        sub day_of_week { my $self = shift; my @days = _('Mon'),_('Tues'),_('Wed'),_('Thurs'),_('Fri') +,_('Sat'),_('Sun'); return $days[ $self->{day_num} ]; }

        To give another example, I have about 3,000 lines of YAML config data, which gets loaded into a hash during initialisation. Some of that data will contain strings-to-be-translated, eg:

        status: a: _('Active') i: _('Inactive')

        During init, I check all the scalar values in the config hash and, if they match the _('...') form, then I bless them into i18n::String. Doing this with tie wouldn't be feasible.

        Clint

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-18 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found