On Mon, May 03, 2004 at 07:17:54PM +0200, Arnaud Blancher wrote:
Stas Bekman a ?crit :
Arnaud Blancher wrote:
Stas Bekman a ?crit :
ydnar wrote:
The eval() is unecessary. The named sub can be used:
&$func();
yes but not with use strict ...
try:
{ no strict 'refs'; &$func(); }
yes, it's work.
i'don't realy like this solution, just because i always try to use 'use strict'
to avoid problem with mod_perl !!!!
As Stas mentioned previously, you should be using coderefs: $func->() instead of eval() with &$func(). If you use eval () with &$func(), then I think you are bypassing some compile-time 'strict' checks on that code. If you want the compiler to enforce maximum strictness (a good thing), and you always try to 'use strict', as you say above, then you should be using coderefs instead of &$func(). An example will better illustrate proper usage:
e.g. if you have the subroutines
sub routine_1 { ... } sub routine_2 { ... }
then instead of doing:
my $func = 'routine_1'; [ ... ] &$func();
try the following:
my $func = \&routine_1; [ ... ] $func->();
That will compile cleanly with 'strict' in effect.
If you do not know the routine name in advance, then you can create a hash of 'routine_name' => coderef and look up the routines to get the proper coderef:
%valid_routines = ('routine_1'=>\&routine_1, 'routine_2'=>\&routine_2);
$func = $valid_routines{$routine_name} || die "invalid routine name";
Good point, Glenn. But it's actually not the style:
&$func() and $func->() are the same strictness-checking-wise.
but the coderef vs. eval "string" that makes the difference. You can just as well do:
perl -wle 'use strict; sub foo { print "X" }; my $func = \&foo; &$func' X
It's personal preference of whether to use $func->() vs &$func, with an added potential pitfall of &$func passing @_ along to the $func function, when () are forgotten. I prefer $func->() since it's clear that no arguments are passed. It's not clear in this code:
use strict; sub foo { print @_ }; sub bar { my $func = \&foo; &$func; }; bar("a")'
as you can see it prints "a", which you may not expect from &$func, unless you remember that &foo is special (this feature is there for 'goto &foo' constructs usually used in AUTOLOAD).
On the other hand:
sub bar { my $func = \&foo; $func->(); };
prints nada. and you can clearly see that no args are passed.
Certainly, you could write:
sub bar { my $func = \&foo; &$func(); };
and @_ won't be passed, but you may forget to add (), you can't forget them in $func->(). :)
-- __________________________________________________________________ Stas Bekman JAm_pH ------> Just Another mod_perl Hacker http://stason.org/ mod_perl Guide ---> http://perl.apache.org mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com http://modperlbook.org http://apache.org http://ticketmaster.com
-- Report problems: http://perl.apache.org/bugs/ Mail list info: http://perl.apache.org/maillist/modperl.html List etiquette: http://perl.apache.org/maillist/email-etiquette.html