3

Unfortunatelly, I did not figure out, why $alfa remains empty:

use 5.014; 
no strict 'refs';

my $berta = 5;
my $gamma = 'berta';
my $alfa = ${$gamma};

say "'$alfa'";

Must be something really simple... I expected $alfa become 5 here. What I missed?

w.k
  • 8,218
  • 4
  • 32
  • 55
  • Your variable names are interesting. Who's berta? :D – simbabque Sep 30 '15 at 08:29
  • 2
    Of relevance to this discussion: http://perl.plover.com/varvarname.html - it is a TERRIBLE idea to ever actually do this. – Sobrique Sep 30 '15 at 09:22
  • possible duplicate of [In Perl, how can I use a string as a variable name?](http://stackoverflow.com/questions/3871451/in-perl-how-can-i-use-a-string-as-a-variable-name) – RobEarl Sep 30 '15 at 10:01
  • 2
    No, it's not a duplicate of that. That's asking how to do it. This question is why is it not working. – Sobrique Sep 30 '15 at 11:12
  • 1
    @simbabque: actually those are spelling names for alphabet letters (example in chess, but in military too), at least in Estonia – w.k Sep 30 '15 at 12:01
  • 1
    I see. Sounded like Greek with a spelling disorder. :) – simbabque Sep 30 '15 at 12:08
  • @w.k, Odd. The [standard](https://en.wikipedia.org/wiki/NATO_phonetic_alphabet) is alfa, bravo and golf. – ikegami Sep 30 '15 at 17:47

1 Answers1

6

I think the problem is that the code only works with global variables, not with lexical variables declared using my. See the FAQ.

our $berta = 5;
 my $gamma = 'berta';
say $$gamma; # 5

Obviously, this is not a good idea in production code.

zoul
  • 102,279
  • 44
  • 260
  • 354
  • Only `$berta`, the dereferenced variable name, must be global. It is OK for `$gamma` to be lexical. – mob Sep 30 '15 at 13:08