In a message dated 2 Sep 2002, Aaron Sherman writes: > I'm working on a library of rules and subroutines for dealing with UNIX > system files. This is really just a mental exercise to help me grasp the > new pattern stuff from A5. > > I've hit a snag, though, on hypothetical variables. How would this code > work? > > { > my $x = 2; > my $y = "The grass is green"; > $y =~ /(gr\w+) {let $x = $1}/; > } > > I assume that it would change C<$x> from C<2> to C<"grass">.
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. > But here's > the case where I'm concerned: > > module foo; > rule gr_word { (gr\w+) {let $x = $1} } > ----my code---- > use foo; > my $x = 2; > "The grass is green" =~ /<gr_word>/; > > Would my local C<$x> be reset to C<"grass">? 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. > That would seem to lead to > some very ugly namespace problems, since the module user never asked to > have C<$x> replaced, it's just a silent side-effect. Nope, shouldn't have any effect. > Please tell me there's some magic way that this works, so that I'm wrong > :) I don't think it's magic. It's just scoping. Trey