and get exactly the same result as I would get from justperl -MO=Deparse try.pl > try2.pl perl try2.pl
Having found a nifty way to get around the problem of being unable to modify a lexical in the same statement as it is declared, I wanted to see how the parser saw it. The original script:perl try.pl
and its output:#!perl use strict; use warnings; use Data::Dumper; s/foo/bar/ for (my $d = 'Shakespear is the food'); print "$d\n";
The script Deparse generated:Shakespear is the bard
and its output:use Data::Dumper; BEGIN {${^WARNING_BITS} = "UUUUUUUUUUUU"} use strict 'refs'; foreach $_ (my $d = 'Shakespear is the food') { s/foo/bar/; } print "$d\n";
What's broken? Deparse, or my understanding of it?Name "main::d" used only once: possible typo at try2.pl line 7. Use of uninitialized value in concatenation (.) or string at try2.pl l +ine 7.
Update: Ok, so what I thought was nifty isn't so nifty. You can't use a lexical by name, but you can modify it easily enough with the usual
Still, the predicate-for trick would give you a variable you could manipulate for something like(my $d = 'Shakespear is the food') =~ s/foo/bar/;
/foo/ and $_ .= '1' for (my $d = 'Shakespear is the foo');
In reply to Deparse broken or just misunderstood? by Roy Johnson
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |