Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Self-writing code revisited

by kcott (Archbishop)
on Sep 06, 2013 at 01:02 UTC ( [id://1052642]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1052642]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found