Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
We don't bite newbies here... much
 
PerlMonks  

using strict refs

by c (Hermit)
on Aug 28, 2001 at 13:43 UTC ( [id://108437]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

I knew this one would be a problem, but I have actually seen it used with use strict no strict refs before. i'd rather not cop out though, so if someone could explain the right way to do it, i'd appreciate it.

#!/usr/bin/perl -w use strict; my $i = "test"; my $test = "success"; print "$$i\n";

humbly -c

Replies are listed 'Best First'.
Re: using strict refs
by bwana147 (Pilgrim) on Aug 28, 2001 at 13:54 UTC

    Locally disable the strict (UPDATE: NO, DON'T! Praises go to Hofmator for correcting this goof):

    #!/usr/bin/perl -w use strict; my $i = "test"; my $test = "success"; { no strict 'refs'; print "$$i\n"; }
    Or use eval:
    #!/usr/bin/perl -w use strict; my $i = "test"; my $test = "success"; print eval qq{"\$$i\\n"};

    --bwana147

      The eval version works, the other version doesn't work because symbolic references only work for package variables - not for lexical variables declared with my. So:

      #!/usr/bin/perl -w use strict; my $i = "test"; # this is now a package variable and requrires an explicit # package name because of use strict $main::test = "success"; { no strict 'refs'; print "$$i\n"; }

      -- Hofmator

Re: using strict refs
by dragonchild (Archbishop) on Aug 28, 2001 at 13:58 UTC
    The question is what are you trying to do? It looks like your trying to create a soft reference. That's exactly what strict 'refs' tries to stop. I would just create a hash that has 'test' as a key and 'success' as a value. Easy enough, huh?

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

Re: using strict refs
by Graham (Deacon) on Aug 28, 2001 at 14:01 UTC
    #!/usr/bin/perl -w use strict; my $i = "test"; my $test = "success"; { no strict 'refs'; print "$$i\n"; }
    should do what you're after

    UPDATE: Beaten to it by bwana147. Must type faster...

    UPDATE: and correctly as Hofmator rightly points out,symbolic references will not work for lexical variables.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://108437]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.