Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Thanks to tachyon's tutorial, I was able to come up with a few modules. This module is used to block access to some of my CGI programs based on IP address.

If you have the time, please take a look it. I'm updating some code for work, and I'd like all the core functions to be in a module. Before I get deeper into it, I'd like to know any pointers you'd be willing to offer. What's good to have in a module and what's not? How can I make OO modules and when should/shouldn't I use OO? Any insight or information you have is greatly appreciated.

#!/usr/bin/perl -w package IPBlock; use strict; use Exporter; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); use constant BLOCKED_LIST => './data/ip_block.dat'; $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = qw(&checkIP); @EXPORT_OK = qw(&checkIP &blockIP &unblockIP &returnIP); %EXPORT_TAGS = (DEFAULT => \@EXPORT, ALL => [qw(&checkIP &blockIP &unblockIP &returnIP)]); sub checkIP { # returns 1 if blocked # returns 0 if not blocked my $ip = shift; open(my $fh, BLOCKED_LIST) or die("Cannot open r BLOCKED_LIST $!") +; flock($fh, 4); my $returnvalue = 0; while(<$fh>){ chomp; if($_ =~ /\A$ip/){ $returnvalue = 1; last; } } close($fh); return $returnvalue; } sub blockIP { my $ip = shift; open(my $fh, '+<', BLOCKED_LIST) or die("Cannot open r/w BLOCKED_L +IST $!"); flock($fh, 2); my @block = <$fh>; chomp(@block); my $returnvalue = 1; if( grep(/\A$ip/, @block) ){ $returnvalue = 0; } if($returnvalue == 1){ push(@block, $ip); seek($fh, 0, 0); truncate($fh, 0); print $fh "$_\n" foreach(@block); } close($fh); return $returnvalue; } sub unblockIP { my $ip = shift; open(my $fh, '+<', BLOCKED_LIST) or die("Cannot open r/w BLOCKED_L +IST $!"); flock($fh, 2); my @block = <$fh>; chomp(@block); my $returnvalue = 0; if( grep(/^$ip/, @block) ){ @block = grep(!/\A$ip/, @block); $returnvalue = 1; } seek($fh, 0, 0); truncate($fh, 0); print $fh "$_\n" foreach(@block); close($fh); return $returnvalue; } sub returnIP{ open(my $fh, '+<', BLOCKED_LIST) or die("Cannot open r/w BLOCKED_L +IST $!"); flock($fh, 4); my @block = <$fh>; close($fh); chomp(@block); return \@block; } 1;

Many thanks,
John J Reiser
newrisedesigns.com


In reply to Advice for moving forward with modules by newrisedesigns

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2024-04-23 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found