This results in:use strict; use warnings; use Data::Dumper; my $hashref = () = { 'ONE' => 1, 'TWO' => 2, 'THREE' => 3, 'FOUR' => 4, }; use constant HASHREFCONST => [$hashref->{ONE} .. $hashref->{FOUR}]; print Dumper(HASHREFCONST);
I'm a little puzzled as to what's going on here. Use directives occur at compile time, so I understand that perl is attempting to set the constant at compile time. However, why does this work as expected, then?Use of uninitialized value in range (or flop) Use of uninitialized value in range (or flop)
I assume the @test array is allocated at compile time, and it's reference is set to GOODCONSTANT also at compile time. Then it's populated at runtime, but the constant is still pointing to the same memory. If this is the case, then it makes sense that my first method won't ever work. Is there a workaround to get the first method to work? Thanks for taking the time to read this!my @test = ($hashref->{ONE} .. $hashref->{TWO}); use constant GOODCONSTANT => \@test;
In reply to Using hashref values in constant declarations. by nameofmyuser
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |