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

If I use : use strict; my script will fail with the following error : Can't use string ("L0") as a SCALAR ref while "strict refs" in use at compgui2.pl line 109. If I declare my $$lentry I got the following error : Can't declare scalar deref in my at compgui2.pl line 29, near "$lentry =" BEGIN not safe after errors--compilation aborted at compgui2.pl line 60. If I don't use strict it works fine. Anybody knows how to get around it and still use strict.

for($i=0;$i<$size;$i++) { if ($size <= 10) { use integer; my $val = $hash{dataParams}->{$keys[$i]}->{val}; my $lentry ='L'.$count; my $$lentry = $W->Label( -text => "$keys[$i]".": $val ", -width => 20, -background => 'white', -foreground => 'black'); Tk::grid($$lentry, -row => $i, -column => 0, -sticky => 'w', -padx => 10, -pady => 10, ); my $closeButton = $W->Button( -text => 'close', -command => [\&exit], -width => 7, -height => 1, ); my $brow = $size + 1; Tk::grid($closeButton, -row => $brow, -column => 0, -sticky => 'w', -padx => 10, -pady => 10, ); }

Replies are listed 'Best First'.
Re: Problems to use strict with dynamic $$variable
by japhy (Canon) on Jun 06, 2001 at 18:05 UTC
    "Doctor, my arm is in a cast."
    "I see."
    "And it hurts when I try to bend my arm in the cast."
    "Well then, don't wear the cast!"

    You're using strict, but doing that which it specifically denies. You shouldn't be using variables like $a0, $a1, $a2, etc., anyway. That's what arrays (or hashes) are for.

    Families of variables are best treated as arrays or hashes, 95% of the time. Just ask Dominus. He gave a great talk at my office last week on "red flags" -- things which should stand out in your code as places of improvement. One such thing was families of variables.

    He also has a great document on his web site about creating variable names dynamically, like you are. It's not a good idea -- again, arrays and hashes are far safer.

    japhy -- Perl and Regex Hacker
Re: Problems to use strict with dynamic $$variable
by davorg (Chancellor) on Jun 06, 2001 at 18:14 UTC

    Please read Dominus' articles on why using a variable as a variable names (aka "symbolic references") is a really bad idea.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: Problems to use strict with dynamic $$variable
by mirod (Canon) on Jun 06, 2001 at 18:09 UTC

    You cannot declare $$lentry. You can only declare scalar, arrays and hashes.

    The usual way to deal with that kind of "dynamic variable" in Perl is to have it as a field in a hash:

    my %entry; my $lentry ='L'.$count; $entry{$lentry}= $W->Label(...

    You don't even need to use the 'L' prefix, as the key in the array need not be a valid identifier.

Re: Problems to use strict with dynamic $$variable
by clintp (Curate) on Jun 06, 2001 at 18:25 UTC
    require "what_everyone_else_said"; If you really, really want to user symrefs with strict in effect (which is usually the only reason I ever _want_ strict in efeect) you can turn off just that feature of stricture where you need it:
    use strict; # Your code here with full stricture in effect { no strict 'refs'; # Code that uses symbolic references confined to # this block. }
Re: Problems to use strict with dynamic $$variable
by tachyon (Chancellor) on Jun 06, 2001 at 18:41 UTC

    With use strict you get use strict 'refs' which specifically prohibits the symbolic references you are trying to use in the case of 'L0'. You need to either use hard references or insert "no strict 'refs'" before you use a symbolic reference. The prohibition of symbolic references under strict is there for a purpose, so you need a good reason to do the second option. See strict and perlref for details.

    use strict 'refs'; my $foo = "bar"; # hard reference my $ref = \$foo; print $$ref; # ok, prints "bar" # symbolic reference $ref = "foo"; print $$ref; # runtime error; ok without strict { no strict 'refs' print $$ref; # works but considered bad }

    The symbolic reference generates runtime error in the first instance. We enclose the no strict 'refs' in a bare block to limit its scope to the one line where we want to use a use a sym ref. This is the best way to do this if you absolutely positively can not live without a sym ref. 99+% of the time you can and should avoid sym refs.

    Your second error occurs because you are trying to declare a dereference $$lentry. You need to declare the variable $lentry, assign a reference to it, and then dereference it using $$lentry. $$lentry is not something that you declare.

    tachyon

(tye)Re: Problems to use strict with dynamic $$variable
by tye (Sage) on Jun 06, 2001 at 20:36 UTC

    <plug>I suggest you read strict.pm to learn more about the benefits of strict.<plug>

            - tye (but my friends call me "Tye")