Jim wrote:
> I've never encountered this before but I have to be doing something wrong.

What you're doing wrong is expecting it to do something different! This is how
it works, I'm afraid.

> snippet of code:
>
> ]$ perl -e '
> > $var = "Company Online (Company Systems) NETBLK-COM-5BLK (NET-24-256-0-0-1)";
> > $var =~ /.*? \(.*\) (.*?) \(.*?\)/;
> > print $1,"\n";
> >
> > $var = "NetBlock: NETBLK-10H-6BLK";
> > $var =~ /sdddd\(.*?\) (.*?) \(.*?\)/;
> > print $1,"\n";
> > '
> NETBLK-COM-5BLK
> NETBLK-COM-5BLK
>
>
> Why isn't $1 getting updated with the next implicit match?  It should fail
> but its returning the first $1 match.

Yes, the match is failing /so/ it's 'returning' the first $1 match. $1 is left
unchanged unless a match succeeds.

> I can't unset $1 because it is a read-only variable.  This doesn't even work
> if I change the second $var to $var2 because of course $1 is the same the
> way through.

Quite so. But if you really need to write stuff like this, then remember that
all the $1 .. $9 values are lexical and so will only be defined through to the
end of a code block. Try this

    {
        $var = "Company Online (Company Systems) NETBLK-COM-5BLK (NET-24-256-0-0-1)";
        $var =~ /.*? \(.*\) (.*?) \(.*?\)/;
        print "> ", $1, "\n";
    }
    {
        $var = "NetBlock: NETBLK-10H-6BLK";
        $var =~ /sdddd\(.*?\) (.*?) \(.*?\)/;
        print "> ", $1, "\n";
    }

output

    > NETBLK-COM-5BLK
    >

which may be what you expected in the first place. However I would encourage
you to check the success status of the regex match to determine your program flow
rather than rely on captured substrings.

HTH,

Rob





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to