Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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

by justinNEE (Monk)
on Feb 28, 2002 at 05:51 UTC ( #148143=perlquestion: print w/replies, xml ) Need Help??
justinNEE has asked for the wisdom of the Perl Monks concerning the following question:

How can I use something like $$a without angering strict refs ?

Also, why does the code below print "1" instead of "2"? I guess I have used $$a before and predicted what the outcome would be, but now its time to find out its really doing. :>
my $a='b'; my $b='1'; $$a='2'; print $b;

Replies are listed 'Best First'.
Re: can't use string as a SCALAR ref while strict refs
by Sweeper (Pilgrim) on Feb 28, 2002 at 06:38 UTC
    Just use a hard reference:
    use strict;
    my $b = '1';
    my $a = \$b;
    $$a = '2';
    print $b;
    You must declare $b before referencing it as \$b (I know, I have tested my code), because typing
    my $a = \$b;
    my $b = '1';
    in this order will autovivify a variable $main::b and then declare a different $b "my" variable.

    The reason why your program prints '1' is nearly the same. You can use symbolic references (opr string references as you call it), only to package variables. You cannot use a symbolic reference to a "my" variable. So, in your code, you have two variables:

    my $b
    That's all !
      Another way to look at this is to ponder
      { my $b = '1'; my $a = 'b'; $$a = 2; print "b = $b\n"; } print "b = $b\n";
      and consider which $b is getting set, and why. This might require a detour into the documentation on what, exactly, 'my' does.

        Symbolic references, which the initial question is about, require entries in the symbol table. "my" does not place entries in the symbol table. This explains the behavior the initial poster asked about, and also answers why you will get
        b = 1 b = 2
        for your output. The setting of $$a to 2 sets the global $b to 2, but inside the block $b refers to the lexical $b with no symbol table entry.
        Hope this helps...
        Other posters have mentioned that what the original poster may really want to use are the hard references...

Re: can't use string as a SCALAR ref while strict refs
by little (Curate) on Feb 28, 2002 at 06:38 UTC
    my $b = '1'; my $a = \$b; $$a = '2'; # ${$a} = '2'; print $b;

    Have a nice day
    All decision is left to your taste
Re: can't use string as a SCALAR ref while strict refs
by Anonymous Monk on Feb 28, 2002 at 09:22 UTC
    my ($a, $b); $a = \$b; #my $a='b'; $b = '1'; #my $b='1'; $a = '2'; #$$a='2'; # Wrong, $$a would dereference $a, but you failed +to create a proper ref in first place. # that's why 'use strict' complained. Nothi +ng should have printed unless you # defeated 'use strict' in which case perl g +uesses that you are trying to # make a reference and correctly prints '1'. print $b; # I assume you are experimenting with references?
      Hi Anony,

      Maybe you didnt notice but your post(s?) havent been formatted and are thus difficult to read and will most likely be ignored. I suggest you have a look at Writeup Formatting Tips.

      Specifically the code tag.

      BTW, stick around and register...


      UPDATE Changed my wording so as not to use the truth as a stick. Apologies if I upset anyone. (Grinder jmcnamara)

      Yves / DeMerphq
      When to use Prototypes?

        Hmm, and you believe that the tone of your post is going to give anonymonk a warm, fuzzy feeling and actually want to register? (for those tuning in late, Yve's orginal words were "Your post looks like crap", or words to that effect).

        It would have been better to just signal the janitors to get them to come over and clean up. It's evident that the poster saw the absence of WYSIWYG, and, human frailty being what it is, glossed over the part about the tips.

        For this kind of thing to occur so often leads me to believe that there is a grain of truth in what Jakob Nielsen has to say, the design of the site is in error, not the user.

        I think the heart of the problem is that most newcomers to the site don't realise that they must write in HTML or perish. While I have only marginal sympathy for a user of a techical site such as PM to understand naught about HTML, I can fully appreciate that there are any number of people who have either the time or inclination to learn it. It's our loss if they have a valuable contribution which is ignored.

        When you are post anything here, there is absolutely no indication at all which suggests that you must use HTML, there is only the tips link. Maybe the Slash plain text/html/extrans method needs to be used. That has the advantage of being understood across a broader range of sites.

Re: can't use string as a SCALAR ref while strict refs
by periferral (Novice) on Jul 08, 2008 at 18:15 UTC
    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.
      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";

      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."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://148143]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2018-04-20 18:53 GMT
Find Nodes?
    Voting Booth?