Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Refactoring Perl 4 - Replace Temp with Query

by agianni (Hermit)
on Jul 27, 2007 at 18:36 UTC ( #629174=note: print w/ replies, xml ) Need Help??


in reply to Re: Refactoring Perl 4 - Replace Temp with Query
in thread Refactoring Perl #4 - Replace Temp with Query

I don't usually use temp variables for that, but that's the idea, if you mean something like:

sub base_price{ my $self = shift; if ( not $self->{_base_price} ){ $self->{_base_price} = $self->{_quantity} * $self->{_item_price}; } return $self->{_base_price}; }

That's how I usually do result caching for methods. There are other options too, like Memoize, although I haven't tried any of the modules that do this.

perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'


Comment on Re^2: Refactoring Perl 4 - Replace Temp with Query
Select or Download Code
Replies are listed 'Best First'.
Re^3: Refactoring Perl 4 - Replace Temp with Query
by akho (Hermit) on Jul 27, 2007 at 18:45 UTC
    Won't work if $self->{_item_price} changes.

    Of course abstracting such things away is the right way to go; but you still often need temp variables. So this refactoring technique should probably be promoted as "do what common sense tells you", not as "remove temp vars".

    Memoize is a good thing too, btw. Even better with Memoize::Attrs.

      Right, up till now, I haven't had to do anything but the simplest forms of Memoization, so my example would generally work for me. I find myself doing memoization most often when I'm doing database queries that I expect should return the same things for the duration of the process, i.e. if handling a Web request. Obviously the situation would be different if one were running a persistent process.

      perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (16)
As of 2015-07-29 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls