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

I have a file with: $r in it. I tried:

#!/usr/bin/perl -w use strict; open(VARNAME, "varname"); my $varname = <VARNAME>; chomp($varname); print "$varname\n"; eval 'my $$varname = 10'; print "$r\n";

and I can't figure out what I am doing wrong. I want $r to exist as a variable in the code,

Replies are listed 'Best First'.
Re: get variable from file to exist within perl code
by 1nickt (Canon) on May 15, 2018 at 13:02 UTC
Re: get variable from file to exist within perl code
by Athanasius (Archbishop) on May 15, 2018 at 13:37 UTC

    Hello mcaongusa, and welcome to the Monastery!

    I can't figure out what I am doing wrong.

    If a my declaration occurs in an eval, its scope extends only to the end of that eval. From the documentation for my:

    A my declares the listed variables to be local (lexically) to the enclosing block, file, or eval.

    But anyway, as 1nickt and hippo have said, what you’re trying to do is a bad idea, and there are better approaches.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: get variable from file to exist within perl code
by hippo (Archbishop) on May 15, 2018 at 13:19 UTC
Re: get variable from file to exist within perl code
by afoken (Chancellor) on May 15, 2018 at 20:30 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.