On Mon, 2 Sep 2002 23:50:18 -0400 (EDT), Trey Harris wrote:
> In a message dated 2 Sep 2002, Aaron Sherman writes:
> > {
> > my $x = 2;
> > my $y = "The grass is green";
> > $y =~ /(gr\w+) {let $x = $1}/;
> > }
>
> Yes. $0{x} would be set to "grass". A lexical variable has been defined
> in the same scope as the hypothetical with the same name, so its value is
> set hypothetically (is hypothetically bound to?) $0{x}. When the rule
> succeeds, $x's hypothetical value is made permanent.
>
> > module foo;
> > rule gr_word { (gr\w+) {let $x = $1} }
> > ----my code----
> > use foo;
> > my $x = 2;
> > "The grass is green" =~ /<gr_word>/;
>
> No. $0{x} would be set to "grass". $x would stay as 2. $x is in a
> different scope from the hypothetical, so it doesn't get touched.
Presumably there is some variant of the strict pragma which would catch
misspellings of $x. Actually, I'd like to see something explicit in the
rule which states whether the hypothetical binding applies to the surrounding
scope as well as to the match variables. Unfortunately, the only way I can
think of doing this is to state $OUTER::x, which is pretty horrible, and
doesn't give the impression that both the external variable and the match
variable are being bound.
Also the different operators used (:= inside the rule, = inside the code)
seems a bit confusing to me; I can't see that they're really doing anything
different:
/ $x := (gr\w+) / vs / (gr\w+) { let $x = $1 } /
Shouldn't they both use C< := > ?
--
Peter Haworth [EMAIL PROTECTED]
> Some more data?
No, no more. Please, no more...
-- Yanick, examining perl's strange behaviour