in reply to can't use string as a SCALAR ref while strict refs

I know this thread is dead but it is the first link on a google search. I'm no expert but I really wanted to do this. My situation is slightly different but related.
I have
my $result = "all";
my $all = "notselected"
my $none = "notselected"

somewhere in my code, I set $result = "none" and still elsewhere I change say $none = "selected". Now, the solution provided here does not help me. I need to get $$result which would be all or none, each of which could be "selected" or "notselected" depending on the path my code takes.
The solution I found that works is
use strict;
no strict 'refs';

This may not be ideal but it works for my scenario. Maybe the experts can chime in.
  • Comment on Re: can't use string as a SCALAR ref while strict refs

Replies are listed 'Best First'.
Re^2: can't use string as a SCALAR ref while strict refs
by runrig (Abbot) on Jul 08, 2008 at 18:45 UTC
    In your specific case, maybe a hash would work (there's almost always a better answer than symbolic references):
    my $result = "all"; my %button = ( all => "notselected", none => "notselected", ); print "$button{$result}\n"; $button{none} = "selected"; $result = "none"; print "$button{$result}\n";
Re^2: can't use string as a SCALAR ref while strict refs
by psini (Deacon) on Jul 08, 2008 at 18:57 UTC

    It's a hack but:

    print eval("\$$result");

    works as expected.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."