in reply to Creating variables while using 'strict'

You can't create global variables when using strict but you can create lexical variables. You do this with 'my'. See Coping with Scoping as well as other entries in the Tutorials section.

Update:BTW, do you have warnings turned on? $$fieldname = ... should have issued a warning with strict turned on.

--- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

Replies are listed 'Best First'.
Re: Re: Creating variables while using 'strict'
by blokhead (Monsignor) on Jan 06, 2003 at 03:58 UTC
    I don't think that will help do what the author intended. The problem isn't creating new variables, it's creating new ones with dynamic names (and then being able to use them under strict). Using lexicals doesn't solve this:
    # trying to create a $dynamic_var use strict; my $var_name = 'dynamic_var'; my $$var_name = 'foo'; # doesn't compile my ${$var_name} = 'foo'; # nor does this eval "my \$$var_name = 'foo';"; # this runs fine, now $dynamic_var # exists in some scope print $dynamic_var, "\n"; # but this doesn't work in strict
    strict requires that variables be predefined before their use, whether global or lexical. However, when dynamically creating variables of any kind, this can't be the case. You may be able to bend things a bit to get the new variables created, but you can't actually use those new variables in the rest of your strict-enabled program (it sounds like nedals wants to use strict as much as possible in the remainder of the code). Please correct me if I'm missing some clever way of creating dynamic lexicals.

    I think a hash is the best way to go.

    blokhead

      Thanks for the clarification blokhead. This is exactly the problem I had in mind.
Re: Re: Creating variables while using 'strict'
by pg (Canon) on Jan 06, 2003 at 03:52 UTC
    Just want to expand a little bit on what pfaut said about $$fieldname.

    First let's look at a piece of correct code:
    use strict; $$a = 1;# you cannot say my $$a ;-) print $$a; #this print out 1; print $a; #this print out SCALAR(0xnnnnnnnn)
    This is perfectly correct, as I just created a hard reference, which is allowed. The next piece is wrong.
    use strict("refs"); my $a = "abc"; my $abc = 1; print $$a;
    This is wrong, as I attempted to create symbolic reference, which is disallowed when you have strict("refs") turned. As a best practice, symbolic reference should be generally disallowed, as it is the source of many bugs in Perl scripts.
Re: Re: Creating variables while using 'strict'
by broquaint (Abbot) on Jan 06, 2003 at 13:11 UTC
    You can't create global variables when using strict but you can create lexical variables
    This isn't strictly true (NPI). You can't use variables that either haven't been declared (see. my|/our and vars) or aren't fully qualified while the strict 'vars' pragma is in use e.g
    shell> perl -c - use strict 'vars'; use vars qw( $foo ); $foo = "package variable declared with use vars"; my $bar = "lexical variable"; $main::baz = "fully qualified package variable"; our $quux = "YAWTDAV"; $::abc = 'shortcut for $main::abc'; ${*{$main::{x}}{SCALAR}} = 'verbosely assign to $::x'; - syntax OK

    HTH

    _________
    broquaint