karlberry has asked for the wisdom of the Perl Monks concerning the following question:
Code to do a basic s//ee (no particular reason to use !! delimiters instead of //, that's immaterial):
use strict; use warnings; my $str = "AxyzB"; my $lhs = qr/xyz/; my $rhs = "qqq"; $str =~ s!$lhs!"$rhs"!ee; print "str=$str\n";
Result:
Use of uninitialized value in substitution iterator at /tmp/try.pl line 5. str=AB
Question: how are substitution iterators involved here? Web searches turn up that term and warning in relation to back references, but there are no back references here, at least not specified by me. Same warning results with s!$lhs!$rhs!ee, that is, no quotes.
On the other hand, there is no warning if I use '$rhs' on the rhs, as in s!$lhs!'$rhs'!ee. (And of course there's no warning with just /e instead of /ee; in the real program, I need /ee.)
My (clearly wrong) intuition was that we would first get the string "qqq" after interpolation of $rhs, and that would eval to itself, or possibly the empty string. Running perl -e 'print eval("qqq");' prints qqq.
I tried both perl 5.24.0 and 5.32.1, same results (not that I expected any difference).
I seek your wisdom. Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: /ee -> Use of uninitialized value in substitution iterator (without back references)
by choroba (Cardinal) on Apr 29, 2021 at 15:14 UTC | |
|
Re: /ee -> Use of uninitialized value in substitution iterator (without back references)
by haukex (Archbishop) on Apr 29, 2021 at 15:14 UTC | |
by karlberry (Sexton) on Apr 29, 2021 at 17:50 UTC | |
by haukex (Archbishop) on Apr 29, 2021 at 18:11 UTC |