[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]
Interpolated code blocks may not create lexicals
Porters,
I just managed to waste 10 minutes after having been bitten by the
following parsing error:
print <<STR;
I like @{[
my $rand = rand();
$rand < 0.5 ? 'pie' : 'beer'
]}
STR
It is illegal, alas, to create a lexical in an interpolated ARRAY ref
within a string. All is not lost since one can use an explicit do block
to set up a pad:
print <<STR;
I like @{[
do {
my $rand = rand();
$rand < 0.5 ? 'pie' : 'beer';
}
]}
STR
I assume that setting up a block on the off chance that someone will do
the above would incur a speed penalty for those cases where lexicals
aren't created (which I would hope is the general case).
I had a look through the documentation (perldata, perlop, perlsyn,
various perldoc -q Interpolation attempts) but didn't find anything.
That's ok, I hardly ever do. Nor do I believe the documentation should
be amended.
What I think should happen is that either my first attempt should Just
Work, or else the error message
syntax error at str line 2, near ");"
syntax error at str line 4, near "'beer'
]"
should give a better hint, like 'cannot create a lexical in an
interpolated code block'.
Beyond that, I have no idea how difficult this would be, and can offer
little more than a TODO test to capture the desired semantics. So I had
another look but didn't find a suitable test file in which to place it,
so I made a new one, attached.
Thanks,
David
--
Stubborn tiny lights vs. clustering darkness forever ok?
interpstr.t
- Follow-Ups from:
-
Nicholas Clark <nick@ccl4.org>
Aristotle Pagaltzis <pagaltzis@gmx.de>
Eirik Berg Hanssen <Eirik-Berg.Hanssen@allverden.no>
[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]