Perl WANNABE wrote:

* John W. Krahn <[EMAIL PROTECTED]> [2008-06-27 11:28]:

Tim Bowden wrote:

On Tue, 2008-06-24 at 19:37 -0700, John W. Krahn wrote:

Perhaps:

my ( $snippet ) = $string =~ /\w\["([^"]+)",/;

$string =~ /\w\["([^"]+)",/;
my $snippet = $1;

does the trick.  I can see If I don't get on top of regex's I'm
seriously restricting the power of perl.  Starting to understand
them better now.

You shouldn't do that.  If you use the value of a numeric variable
without verifying that the regexp matched successfully then the
value in the numeric variable could be the result of a previous
successful match.

That's a GREAT point John but how can you verify that the regexp
matched successfully ?

Using the previous example:

my $snippet;
if ( $string =~ /\w\["([^"]+)",/ ) {
    $snippet = $1;
    }

Or:

$string =~ /\w\["([^"]+)",/ and my $snippet = $1;

Or:

my ( $snippet ) = $string =~ /\w\["([^"]+)",/;


How do you know that the $1 does NOT come from your last regexp, you
can't undefine it BEFORE you use it ?

No, numeric variable are read-only and cannot be modified outside of a regular expression.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to