#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11117224 use warnings; my $match = qr[([ab]+)([ab]+)]; my $str = 'aba'; $str =~ /^ $match $ (?{ print "1: $1-$2\n" }) [c] /x; $str =~ /^ $match $ (?{ print "2: $1-$2\n" }) (?!) /x; $str =~ /^ $match $ (??{ print "3: $1-$2\n"; qr[(?!)] }) /x; # The fact that this is a defined pattern probably means # there is a lesser chance of it being optimized away. $str =~ /^ $match $ (?{ print "4: $1-$2\n" }) (*FAIL) /x;