Subject: | Bind at runtime can cause "Modification of a read-only value attempted" error |
This code produces a "Modification of a read-only value attempted" error
in the Inline::bind method:
1: use strict qw(vars subs);
2: for ('function') {
3: $_->();
4: }
5:
6: sub function {
7: require Inline;
8: Inline->bind(C => <<'__CODE__');
9: void foo()
10: {
11: }
12: __CODE__
13: }
With '-MCarp::Always', the output is:
Modification of a read-only value attempted at
/usr/lib/perl5/site_perl/5.10/Inline/denter.pm line 191
Inline::denter::_setup_line('Inline::denter=HASH(0x101a4940)')
called at /usr/lib/perl5/site_perl/5.10/Inline/denter.pm line 30
Inline::denter::undent('Inline::denter=HASH(0x101a4940)',
'version : 0.45\x{d}\x{a}languages : %\x{d}\x{a} C : C\x{d}\x{a}
Foo : Foo\x{d}\x{a} ...') called at blib/lib/Inline.pm (autosplit
into blib/lib/auto/Inline/check_config_file.al) line 704
Inline::check_config_file('Inline=HASH(0x1013a890)') called at
/usr/lib/perl5/site_perl/5.10/Inline.pm line 244
Inline::glue('Inline=HASH(0x1013a890)') called at
/usr/lib/perl5/site_perl/5.10/Inline.pm line 182
Inline::bind('Inline', 'C', 'void foo()\x{a}{\x{a}}\x{a}')
called at inline_issue.pl line 8
main::function1() called at inline_issue.pl line 3
I see there are functions in the Inline module, including
Inline::denter::_next_line, Inline::denter::_setup_line,
Inline::denter::indent, and Inline::C::get_types, that overwrite the
value of $_ without localizing or lexically scoping it. When $_ is used
in a way that makes it a read-only value, as it is on line 3 of the test
script, the error occurs.
Now that I know what causes this problem, it is easy to workaround it in
the code that calls Inline, but it would also be possible to prevent
this problem inside the Inline module.