One solution:
use strict;
use warnings;
use URI qw( );
my @insert_words = qw( HELLO );
while (<DATA>) {
chomp;
my $url = URI->new($_);
my $path = $url->path();
for (@insert_words) {
# Use package vars to communicate with /(?{})/ blocks.
local our $insert_word = $_;
local our @paths;
$path =~ m{
^(.*/)([^/]*)((?:/.*)?)\z
(?{
push @paths, "$1$insert_word$2$3";
if (length($2)) {
push @paths, "$1$insert_word$3";
push @paths, "$1$2$insert_word$3";
}
})
(?!)
}x;
for (@paths) {
$url->path($_);
print "$url\n";
}
}
}
__DATA__
http://www.stackoverflow.com/dog/cat/rabbit/
http://www.superuser.co.uk/dog/cat/rabbit/hamster/
http://10.15.16.17/dog/cat/rabbit/