in reply to Problems evaluating a user supplied regex using eval

this line from perlvar about $& should assist :
This variable is read-only and dynamically scoped to the current BLOCK


Guess what an eval counts as?

This will also affect the $n variables.

If you plan on matching only, you might take a look at evaling the creation of the regex, something like (off the top of my head) :
eval ($re=qr/foobar/) || warn $@;
or
if (eval ($re=qr/quuzy/)) { # manipulate } else { #blargh! bad re }

Replies are listed 'Best First'.
Re: Re: Problems evaluating a user supplied regex using eval (boo)
by Popcorn Dave (Abbot) on Apr 24, 2002 at 18:50 UTC
    Thanks for the reply!

    So since eval is scoped to it's block, then am I correct in assuming I could do something along the lines of

    eval { $my_text =~$regex # where $regex is m/ab|(cd)/ for example print $&; print $1, $2 # if defined }

    and then $& will hold the value of the match, and $1 and $2 will be accessable?

    My understanding of this, and please correct me if I'm wrong, is that since eval is dynamically scoped, as long as my code is blocked between {} I'm okay.