Subject: | objrule forcefully injects context string within object internals |
<objrule: Some::Class> calls the ->new() method on that class, as
documented ... but after that, it forcefully injects the context string
under hash key "" into that blessed object! This is bad because
a) the object's internals contain data that the object author never
intended to find there
b) the object has to be a hashref; if you want to bless something else
(an arrayref or a scalarref), then you get an error.
Below is a small program to illustrate that case. Curiously, playing
with the <nocontext:> directive, it seems to be possible to remove some
context strings, but not all of them.
===========================================
use strict;
use warnings;
use Regexp::Grammars;
use YAML qw/Dump/;
my $grammar = qr[
<nocontext:> # only works for top-level
<Foo>
<objrule: Tst::Foo>
<nocontext:> # only worlds for this level (top)
FOO \( <a=Bar> , <b=Bar> AND <c=Bar> \)
<objrule: Tst::Bar>
<nocontext:> # doesn't work at all
BAR <num=(\d+)>
]xms;
my $string = "FOO (BAR 1, BAR 2 AND BAR 3)";
$string =~ $grammar
and print Dump $/{Foo};
package Tst::Foo;
sub nonew {
my ($class, $parsed) = @_;
my $self = {};
$self->{"attr_$_"} = $parsed->{$_} for qw/a b c/;
bless $self, $class;
}
package Tst::Bar;
sub nonew {
my ($class, $parsed) = @_;
# can't have arrayrefs as objects: line below creates an error
# my $self = [$parsed->{num}];
# so let's just use a regular object
my $self = {val => $parsed->{num}};
bless $self, $class;
}