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


In reply to Re: Self-writing code revisited by kcott
in thread Self-writing code revisited by Statue

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.