On Wed, 20 Dec 2000, Alexander Farber (EED) wrote:

> Hi,
> 
> http://perl.apache.org/guide/perl.html#my_Scoped_Variable_in_Nested_S
> advises not to use external "my $variables"
> from subroutines. I have
> the following subroutine in my CGI-script, which I would like to keep
> mod_perl-kosher, just in case:
> 
> ################################################################################
> # Sort %hoh-values by the 'sort'-parameter or by default ("MHO")               #
> ################################################################################
> 
> sub mysort
> {
>     my $param = $query -> param ('sort') || 'MHO'; # XXX global $query,
>                                                    # not mod_perl clean?
>     return $a -> {$param} cmp $b -> {$param};
> }
> 
> This subroutine is called later as:
> 
>     for my $href (sort mysort values %$hohref)
>     {
> ...
>     }

Your code is better written as:

  my $param = $query->param('sort') || 'MHO';
  for my $href (sort {$a->{$param} cmp $b->{$param}} values %$hohref) { }

why wasting resources...

> Is using the "outside" $query dangerous here and how would you handle it?

Yes, inside the script. Read again
http://perl.apache.org/guide/perl.html#my_Scoped_Variable_in_Nested_S

> PS: Is there something to be aware of, when using the new "our" keyword?

our == use vars, which declares global variables.


_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide 
mailto:[EMAIL PROTECTED]   http://apachetoday.com http://logilune.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/  


Reply via email to