You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Aaron Johnson <so...@gina.net> on 2003/05/08 15:31:16 UTC
Re: Speed of "if,elsif" versus $hash ...
On Thu, 2003-05-08 at 05:18, Jim Morrison [Mailing-Lists] wrote:
> I just had a thought.. Maybe it's not a great one, but I'd appreciate
> some guidance..
>
> I tend to start my scripts with a big if like :
>
> if ( $method eq 'foo' ){ do_something(); }
> elsif ( $method eq 'bar' ){ do_somethingelse();}
> etc..
>
> It's not too uncommon for this to become quite a big if,elsif,elsif ..
> Which is ok I guess cos it's only one and it determines the whole of the
> rest of the operation of the cgi.. .. But I've noticed another 'sub' I
> have that returns path's to files acording to standard rules that is
> also becoming quite big.. It follows the same rules :
>
> sub path{
> my $self = shift;
> my $path = shift;
> if ($path = 'www'){ return '/home/' . $self->{domain} .
> '/www/'; }
> elsif ($path = 'foo'){ return '/bar/foo/'; }
> etc..
> }
>
> This might get called quite frequently.. So what I'm wondering is, if I
> were to make a hash :
>
> my %paths = ( www => \sub{ return '/home/' . $self->{domain} .
> '/www/' },
> foo => '/bar/foo',
> etc => ....
> )
>
> Then looking up the path would be much quicker right?? Because if it's a
> long if,elsif,elsif then I'd have to itterate through it every time,
> whereas if it's a hash I don't?? (I'm not too sure of the syntax of
> putting the 'sub' in, is that right?)
>
> Anyway - the way I figure it is that any time you've got an if,elsif
> statement that get's called a lot, or is particularly long, you'd be
> much better of predefining it as a hash of references to functions
> because it would resolve much faster.. - have I got this wrong or does
> everyone else know this already? ;-)
>
> Only other thing that bothers me is, in the first example I can quite
> happily use $self->{domain} to get something out of the $self passed to
> the function.. In the hash example - I'm not sure I understand the scope
> of the 'sub' .. Can I pass it arguments, and how do I get to $self??
>
You could do something like this:
package test;
my $obj = {};
# hardcoded for example
$obj->{domain} = 'mydomain.com';
bless $obj , 'test';
# _domain_ is a placeholder, see below
my %paths = ( www => '/home/_domain_/www/',
foo => '/bar/foo',
etc => '/home/user'
);
sub path {
my $self = shift;
my $dir = shift;
my $path = $paths{$dir};
# our placeholder matches our object value name
if ($path =~ /_(\w+)_/) {
my $val = $1;
$path =~ s/_$val\_/$self->{$val}/;
}
return $path;
}
foreach (keys %paths) {
print "$_ = " , $obj->path($_) , "\n";
}
The '_' on the placeholder could be replaced with anything unique enough
for the regex.
Aaron
> Cheers,
> Jimbo..
>
>
>
>
>
>
>
> _____________________________
> Jim Morrison
> Isotope Communications
> www.mediaisotope.com
>
>
>
RE: Speed of "if,elsif" versus $hash ...
Posted by "Jim Morrison [Mailing-Lists]" <ju...@mediaisotope.com>.
Thanks all..
Soz it's a bit OT..
TTFN, J.