Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (15)
    As of 2015-07-29 19:17 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (267 votes), past polls