neshura has asked for the wisdom of the Perl Monks concerning the following question:
According to the Sacred Book (p. 209), "One never needs to force evaluation in a list context, because any operation that wants a list already provides a list context to its list arguments for free." But what if one is particularly ornery? Does anyone know a 1-line hack to get around this particular hashkey-regex instance?$nerf{"my"} = "fridge"; $nerf{"0"} = "truth"; $nerf{"1"} = "beauty"; @nerf = ('i', 'love', 'cheese'); $_ = 'my funny valentine'; print "print ctxt 1: ", /^(\w*)/, " $&", "\n"; # good - this regex returns the # match b/c it's a list context print "print ctxt 2: ", /^\w*/, " $&", "\n"; # good - this regex returns a # 1 b/c there are no parentheses $x = $nerf{/^(\w*)/}; print "hash ctxt: ", $x, " $&", "\n"; # this regex returns a 1 despite the # parentheses because it wants a scalar # but dammit, i'd like to be able to do regexes # within hash keys $y = $nerf[/^(\w*)/]; print "array ctxt: ", $y, " $&", "\n" # this regex also returns a 1 despite the parentheses
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Contexts for regex evaluations
by Anonymous Monk on Feb 10, 2000 at 17:11 UTC | |
|
RE: Contexts for regex evaluations
by mikfire (Deacon) on Feb 10, 2000 at 19:52 UTC | |
by neshura (Chaplain) on Feb 10, 2000 at 22:46 UTC |