Stas et al,
OK. Gave this a try:
file:MyApache/BlockByIP.pm
--------------------------
package MyApache::BlockByIP;
use strict;
use warnings;
use Apache::RequestRec ();
use Apache::Connection ();
use Apache::Const -compile => qw(FORBIDDEN OK);
my %bad_ips = map {$_ => 1} qw(127.0.0.1 10.0.0.4);
sub handler {
my $r = shift;
return exists $bad_ips{$r->connection->remote_ip}
? Apache::FORBIDDEN
: Apache::OK;
}
1;
I'm pretty OK with this, but let me ask two questions:
Question 1: When I commented:
# use Apache::Connection ();
I got an error:
[Sat Jul 03 17:21:31 2004] [error] [client 127.0.0.1] Can't locate object method "remote_ip" via package "Apache::Connection" at /home/darnold/modperl/MyApache/BlockByIP.pm line 17.\n
But when I commented:
# use Apache::RequestRec ();
No error! How come?
That means that some other module that you load, loads Apache::RequestRec, but that doesn't happen for Apache::Connection.
You need both:
% lookup connection To use method 'connection' add: use Apache::RequestRec ();
% lookup remote_ip To use method 'remote_ip' add: use Apache::Connection ();
% alias lookup perl -MApache2 -MModPerl::MethodLookup -e print_method
See http://perl.apache.org/docs/2.0/api/ModPerl/MethodLookup.html
Question 2: I am shaky with this line:
use Apache::Const -compile => qw(FORBIDDEN OK);
What's that "compile" thingy and can someone break down the use of the => notation in this situation for me?
mp2 comes with several hundreds of constants, which you don't want to make available to perl by default, both due to CPU and memory overhead. So you need to ask for one when you want it.
use Apache::Const -compile => qw(FORBIDDEN OK);
makes the constants Apache::FORBIDDEN and Apache::OK available to your code, but they aren't imported. If you drop -compile and write:
use Apache::Const qw(FORBIDDEN OK);
The both constants are imported into your code's namespace and can be used standalone like so:
return OK;
Both, due to the extra memory requirement when importing symbols and since there are constants in other namespaces (APR:: and ModPerl::) which it may contain the same names, it's not recommended to import constants.
Finaly, => is the same as , in perl, and used a syntax sugar to denote a key/val pair. So instead of writing:
use Apache::Const -compile => qw(FORBIDDEN OK);
You could write:
use Apache::Const "-compile", qw(FORBIDDEN OK);
and for parentheses-lovers:
use Apache::Const ("-compile", qw(FORBIDDEN OK));
I'll add this explanation at: http://perl.apache.org/docs/2.0/api/Apache/Const.html#Synopsis Let me know if there is a need for further explanation.
-- __________________________________________________________________ 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