#!/usr/bin/perl -w use v5.20; use re 'strict'; $_ = 'my$a=5;'; my $re = qr/ (?(?&statement)*) (?(DEFINE) (? my \$ (?&identifier) = \d ; ) (? [a-z] ) )/x; $_ =~ $re; say $+{statements}; # Assuming I do the $^R and $^N stuff to create a parse tree, then it would be nice, if I could write something like: # ${$r->{statements}[0]->{identifier}} = 'b'; # in order to change $_ to "my$b=5;" # similar in spirit to substring references: # my $s = \substr $_, 3, 1; # $$s = 'b';