Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Best Practice for replace sub

by Anonymous Monk
on Mar 01, 2013 at 16:05 UTC ( #1021290=note: print w/ replies, xml ) Need Help??


in reply to Best Practice for replace sub

I'd put the encoding-work in a subroutine and give no further thought to initializing and tearing-down the variable. Unless you are truly doing this millions of times such that you can clearly demonstrate that there's a problem to be solved here, you have no problem. The code should be clear and well tested and easily changed, with low chances of compromising some other part of the system by doing so. This to me is an argument against "globals."


Comment on Re: Best Practice for replace sub
Re^2: Best Practice for replace sub
by tobyink (Abbot) on Mar 01, 2013 at 16:56 UTC

    In HTML::HTML5::Entities I've got a hash with 2231 entries. It would be wasteful in the extreme to rebuild that hash every time the encode_entities function is called.

    Closing over a lexical variable is a perfectly safe thing to do.

    (As it happens, I use a package variable for HTML::Entities because that's what HTML::Entities does, and the aim is to provide a compatible API.)

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-07-22 21:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (129 votes), past polls