Re: writing code that works on machines with or without mod_perl
On Thu, 18 May 2000, Matt Sergeant wrote: On Thu, 18 May 2000, Kenneth Lee wrote: modperlers, does it make sense if i put some mod_perl specific codes inside an eval() so that the code runs on machines that have or haven't mod_perl installed? eval 'MOD_PERL_CODE' if $ENV{MOD_PERL}; use Apache (); my $r = Apache-request; ... MOD_PERL_CODE Better still: eval { die unless $ENV{MOD_PERL}; require Apache; my $r = $Apache-request; ... }; Then you've got no (at least much less than the above) run-time overhead. better still: use constant IS_MODPERL = $ENV{MOD_PERL}; BEGIN { import Apache::Constants qw(OK) if IS_MODPERL; } if (IS_MODPERL) { my $r = Apache-request; } _zero_ runtime overhead, since IS_MODPERL is constant folded, that block is optimized away at compile time outside of mod_perl. you shouldn't need to 'use Apache ()', mod_perl does that for you, along with Apache::Constants. in any case, have your startup script require any Apache:: modules you need and import in a BEGIN block if needed.
writing code that works on machines with or without mod_perl
modperlers, does it make sense if i put some mod_perl specific codes inside an eval() so that the code runs on machines that have or haven't mod_perl installed? eval 'MOD_PERL_CODE' if $ENV{MOD_PERL}; use Apache (); my $r = Apache-request; ... MOD_PERL_CODE if i don't do so, perl will complain about Apache is not installed on machines that doesn't have mod_perl installed. kenneth
Re: writing code that works on machines with or without mod_perl
On Thu, 18 May 2000, Kenneth Lee wrote: modperlers, does it make sense if i put some mod_perl specific codes inside an eval() so that the code runs on machines that have or haven't mod_perl installed? eval 'MOD_PERL_CODE' if $ENV{MOD_PERL}; use Apache (); my $r = Apache-request; ... MOD_PERL_CODE Better still: eval { die unless $ENV{MOD_PERL}; require Apache; my $r = $Apache-request; ... }; Then you've got no (at least much less than the above) run-time overhead. -- Matt/ Fastnet Software Ltd. High Performance Web Specialists Providing mod_perl, XML, Sybase and Oracle solutions Email for training and consultancy availability. http://sergeant.org http://xml.sergeant.org
Re: writing code that works on machines with or without mod_perl
arggg... i was sticked to "use" instead of "require"... but how about if i've to import something? Matt Sergeant wrote: On Thu, 18 May 2000, Kenneth Lee wrote: modperlers, does it make sense if i put some mod_perl specific codes inside an eval() so that the code runs on machines that have or haven't mod_perl installed? eval 'MOD_PERL_CODE' if $ENV{MOD_PERL}; use Apache (); my $r = Apache-request; ... MOD_PERL_CODE Better still: eval { die unless $ENV{MOD_PERL}; require Apache; my $r = $Apache-request; ... }; Then you've got no (at least much less than the above) run-time overhead. -- Matt/ Fastnet Software Ltd. High Performance Web Specialists Providing mod_perl, XML, Sybase and Oracle solutions Email for training and consultancy availability. http://sergeant.org http://xml.sergeant.org
Re: writing code that works on machines with or without mod_perl
i know that, but it doesn't work if i use "use", since the block will be eval()'d at compile time: eval { die unless $ENV{MOD_PERL}; use Apache::Constants qw(:common); ... }; it complains if Apache::Constants is not installed. Matt Sergeant wrote: On Thu, 18 May 2000, Kenneth Lee wrote: arggg... i was sticked to "use" instead of "require"... but how about if i've to import something? perldoc -f use
Re: writing code that works on machines with or without mod_perl
On Thu, 18 May 2000, Kenneth Lee wrote: i know that, but it doesn't work if i use "use", since the block will be eval()'d at compile time: eval { die unless $ENV{MOD_PERL}; use Apache::Constants qw(:common); ... }; it complains if Apache::Constants is not installed. Since you didn't read the docs, here they are (relevant bit highlighted): % perldoc -f use =item use Module LIST =item use Module =item use Module VERSION LIST =item use VERSION Imports some semantics into the current package from the named module, generally by aliasing certain subroutine or variable names into your package. It is exactly equivalent to BEGIN { require Module; import Module LIST; } ^^^ except that Module Imust be a bareword. -- Matt/ Fastnet Software Ltd. High Performance Web Specialists Providing mod_perl, XML, Sybase and Oracle solutions Email for training and consultancy availability. http://sergeant.org http://xml.sergeant.org