in reply to Self-writing code revisited
G'day Statue,
Welcome to the monastery.
That's illegal; it's got nothing to do with the array:
$ perl -E ' my $x = "z"; my $y = 1; my $$x = $y; say $z; ' Can't declare scalar dereference in "my" at -e line 4, near "$x =" Execution of -e aborted due to compilation errors.
If you didn't want a my variable, you could do this:
$ perl -E ' my $x = "z"; my $y = 1; $$x = $y; say $z; ' 1
You could eval a string containing that code but the scope of the lexical variable so declared would only be that string:
$ perl -E ' my $x = "z"; my $y = 1; eval "my \$$x = \$y; say \">>>\$z<<<\""; say ">>>$z<<<"; ' >>>1<<< >>><<<
If you're actually trying to write code that writes code, your best bet might be to output the generated code to a file (e.g. script_my_script_wrote.pl) which you could subsequently run in the normal fashion. For example, this code in your generating script:
print $out_script_fh "my \$$splitter[0] = $splitter[1];";
Would write this line into script_my_script_wrote.pl (or whatever you call it):
my $testval = 1;
[Aside: this is your first post so the title "Self-writing code revisited" doesn't make much sense. Perhaps a link to whatever you're revisiting would help to clarify what you mean.]
-- Ken
|
|---|