Sumo Wrestler (or just ate too much) wrote:
> 
> Try something like this:
> 
> use strict;
> use Data::Dumper;
> 
> $var = '
> iptables -A INPUT -p tcp -s 123.45.678.90 --dport 22 -j ACCEPT
> 
> and ...
> 
> iptables -A INPUT -p tcp --dport 25 -j ACCEPT
> ';
> 
> open (FH, '<', \$var)
>     or die ("Couldn't open in memory string: $!\n");

Why not just use __DATA__ and the DATA filehandle?

> # Important spaces have to be backslashed because I'm using
> # the /x option to make this regex readable.
> my $rx = q{

Why not just use qr{}x?

>     (INPUT)                     # chain name
>     \ -p\ (\w+)                 # protocol name
>     \ (?:-s\ ([.\d]+))?         # source ip optional (?)
>     \ ?                         # extra space, optional (?)
>     --dport\ (\d+)              # destintation port
>     \ -j\ (\w+)                 # target
>     };
> 
> my $expand = sub {

Why not just use a sub named 'expand' instead of a reference?

>     my @fields = qw(chain proto sport dport target);
>     my $fi = 0;
>     my %hash = map +($fields[$fi++], $_), @_;
>     return %hash;

Subroutines flatten their return values to a list so why not just return that
list instead of copying it to a hash first?

sub expand {
    map { $_ => shift @_ } qw( chain proto sport dport target );
    }

> };
> 
> while (<FH>) {
>     chomp;

Why chomp?  The (\w+) at the end of the pattern is not going to capture the
newline.

>     next unless m/^iptables/;
>     s/\s+/ /g;

Why not just use \s+ in the pattern?

>     if (m/$rx/ixo) {

You are using the /i option so that means that -p is the same as -P, -s is the
same as -S, -j is the same as -J and --dport is the same as --DPORT.

>         my %hash = $expand->($1,$2,$3,$4,$5);
>         print Dumper(\%hash);
>     }
> }
> 
> close (FH);


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to