in reply to Re^4: Create a ipset for blocking networks based on internet sources
in thread Create a ipset for blocking networks based on internet sources

qr/^((\d{1,3}\.){3}\d{1,3})/m

The problem there is that you have two sets of capturing parentheses.    You can change the inner capturing parentheses to non-capturing parentheses:

qr/^((?:\d{1,3}\.){3}\d{1,3})/m

And you could change the foreach loop:

foreach ( $response->content =~ /$regex/g ) { @sys = (qw(ipset add), "temp_$set_name", $_); system(@sys) == 0 or die "Unable to add $_ to temp_$set_na +me because: $?"; }

To a while loop that only accesses $1:

while ( $response->content =~ /$regex/g ) { @sys = (qw(ipset add), "temp_$set_name", $1); system(@sys) == 0 or die "Unable to add $_ to temp_$set_na +me because: $?"; }

Replies are listed 'Best First'.
Re^6: Create a ipset for blocking networks based on internet sources
by mimosinnet (Beadle) on Apr 27, 2012 at 17:23 UTC

    Thanks for the tip on the use of the extended pattern (?: ) to not make backreferences!

    It has been also very enlightening to see the relationship between the foreach loop and the $_ variable, and what would be the equivalent of the while loop and the $1 bakcreference.

    Your second suggestion has made me explore more how the while loop works, realizing that while ( $response->content =~ /$regex/g ) gets the content each time that the loop is called. I have changed it by:

    my $resp = $response->content; while ( $resp =~ /$regex/g ) { @sys = (qw(ipset add), "temp_$set_name", $1); }

    Your comments have been very useful! Although all these issues are on the documentation, it is sometimes difficult at the beginning to find the right section at the right time. It has been great to apply it to a specific example. Very greatful!

    Cheers!