Statue has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks:


I was wondering if there was a way to do this (you will get the idea when you see the code - if not, I'll explain below):


#!/usr/bin/perl -w use strict "vars" ; my $data ; my @splitter ; $data = "testval,1" ; @splitter = split( "," , $data ) ; my $$splitter[ 0 ] = $splitter[ 1 ] ;

I know you can do something like this with hashes, but I don't want to use hashes. Here's the gist of what I am trying to do with this code. I have data (defined here as $data, but will be in a file). It is comma-separated. The first value in this data is the actual variable name I want to define. The second value is the value I want to assign to it. Obviously, assigning the value is easy, but creating the variable is not so easy. The $$ makes sense in terms of real-time substitution, however it doesn't work. Perl is interpreting it to mean something other than a string declaration. The error message given is:


Can't declare scalar dereference in "my" at srtest line 7, near "$spli +tter[" syntax error at srtest line 7, near "$splitter[" srtest had compilation errors.

Any ideas of how to make this work?


Statue

Replies are listed 'Best First'.
Re: Self-writing code revisited
by kennethk (Abbot) on Sep 05, 2013 at 23:44 UTC
    I know you can do something like this with hashes, but I don't want to use hashes.
    Requisite warnings: Why it's stupid to use a variable as a variable name. Seriously, read all three pages.

    But since you asked, you are looking for Symbolic references. Your code would be written as

    #!/usr/bin/perl -w use strict "vars" ; my $data ; my @splitter ; $data = "testval,1" ; @splitter = split( "," , $data ) ; ${$splitter[ 0 ]} = $splitter[ 1 ] ; print ${$splitter[ 0 ]}, "\n";

    where I added a print for fun. This creates a maintenance headache, and note that your $testval variable is never associated with a declaration (my or our), so if the last line were print $testval, "\n"; you'd get a compile time error from strict 'vars'.

    Also note that what I've written creates a package variable, not a lexical variable. This means you might have weird scoping issues pop up depending on what you are actually doing.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Self-writing code revisited
by kcott (Archbishop) on Sep 06, 2013 at 01:02 UTC

    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