Looping backreferences

Brad Bowman melbourne.pm at bowman.bs
Thu Aug 21 04:13:20 CDT 2003

> I would like (for no other reason than shorter lines and out of curiousity)
> to be able to more intuitively write
> $net = join ".", reverse $zone =~ /^(?:(\d+)\.){2,3}in-addr\.arpa$/;

Why not capture the whole "1.168.192"?

($_) = $zone =~ /^((?:\d+\.){2,3})in-addr\.arpa$/; 
$net = join '.', reverse split '\.';

It is more verbose but the {2,3} is preserved

> Anyone know if/how it's possible to have a variable-length list returned by 
> a regex? (preferably 5.6 compat)

I can't think of a way, but its not too hard to
list them out or build the regex with interpolation.

I thought maybe (??{}) could do it but it looks like
the capture grouping can't be deferred until runtime.
    perl -Mre=debug -e '/(...)/'
    perl -Mre=debug -e '/(?{"(...)"})/'

Other ideas:

print $1 while /\G(\d+)\./g;

/(?: (\d+)\. (?{ print "$1\n" }) ){2,3}/x

1 while s/^(\d+)\.(.*)/$2.$1/;


  It is a wretched thing that the young men of today are so contriving
  and so proud of their material possessions. Men with contriving hearts
  are lacking in duty. Lacking in duty, they will have no self respect.
     -- Hagakure http://bowman.bs/hagakure/

More information about the Melbourne-pm mailing list