Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm writing a little ban routine for a message board. I am having trouble with the subroutine to remove banned ip. My code removes the bad IP entry and all values below it. I am storing the values in a text file. All help gratefully received. I am very new to Perl so would appreciate a simple reply :)
#!/usr/bin/perl print "Content-type: text/html\n\n"; & read_input; $bad_ip = $FORM{'banip'} ; unless ($bad_ip =~ /^\d+.\d+.\d+.\d+$/) { print "<font face=Verdana size=2>That IP looks Invalid!"; print"<br><br>\n"; print "<div align = center><a href=\"javascript:history.go(-1)\">Go B +ack and Try Again</a></div>"; exit; } unless (-e "ip.txt") {print "There are no banned IP's<br>"; exit; } open (IP, "<ip.txt"); @ip= <IP>; close (IP); open (IP, ">ip.txt"); foreach $_(@ip) { chomp $_; if (/$bad_ip/) {next}; chomp$_; print IP "$_\n"; close (IP); } print "All Done!<br>"; print"<br><br>\n"; print "<div align=center><a href=cp.htm>Back to the menu here</a>"; sub read_input { if ($ENV{'REQUEST_METHOD'} eq 'POST') { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } }

Replies are listed 'Best First'.
(ar0n: grep/splice) Re: Deleting a value in a list
by ar0n (Priest) on Nov 29, 2001 at 21:53 UTC
    And now to answer your question:
    @array = grep !/$bad_ip/, @array;
    But you should really should title your nodes better, since I almost wanted to answer with this:
    splice @array, $index, 1;
    Granted, I should've read your question before I posted :)

    [ ar0n -- want job (boston) ]

Re: Deleting a value in a list
by George_Sherston (Vicar) on Nov 29, 2001 at 21:24 UTC
    Somebody's going to say this, and I may say it more tactfully than some others: may I encourage you to use strict and warnings. There are so many reasons why this is a good thing. I used to poo-poo them all but over time my code got claggier and slower and in the end when I made the effort, there was hardly any pain and a sudden improvement in programming style and reliability.

    One other advantage: it would probably make your question easier to answer. As it stands you've posted a longish piece of code, and it's not a costless endeavour to go through it and work out where it breaks. But if you ran it under strict with warnings enabled, then when it broke you could look at your server error log and it wd likely give you a handy line-referenced message telling you where the problem was. Or if you don't have access to the error log and you can put the following at the top of your code:
    use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI qw(:standard); print header; warningsToBrowser(1);
    Then it sends the error messages to your browser (warnings will show up as HTML comments).

    And... then you'll be running CGI.pm which you can use to make yourself MUCH HAPPIER. It really does make sense - I speak as someone who's tried both ways. If you're unconvinced - use CGI or die;!

    At the end of that, if you still can't figure out the problem with all the help that strict and its pals are giving you, then post again and you'll get a complete answer.

    § George Sherston
Re: Deleting a value in a list
by tune (Curate) on Nov 29, 2001 at 21:35 UTC
    Yeah I agree with George, and use CGI as well. There is not the time of read_input() subroutines anymore...

    --
    tune

Got it
by Anonymous Monk on Nov 30, 2001 at 01:07 UTC
    Thanks for the help and I agree with the points about strict and warnings...I will make use of them. Just very new to Perl and don't really have a style yet. I have the code working now thanks.