http://qs321.pair.com?node_id=358010

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

Hi,
I have finally been converted to 'use strict' and now I am learning how to use it properly.
Is it possible to do the following (somehow) while using strict?
#!/usr/bin/perl my @interests = ( "foo", "bar", "blah" ); foreach (@interests) { $$_ = "My interest is $_"; } foreach (@interests) { print $$_ , "\n"; } exit;
It works well without but returns errors when I 'use strict'.
Cheers,
Reagen

Update: I wasnt so much interested in solving this problem, more so in the how/why it doesnt work.
Thanks everyone for your help - I'll try and do a bit more research on symbolic references.

Replies are listed 'Best First'.
Re: use strict
by Abigail-II (Bishop) on Jun 01, 2004 at 12:58 UTC
    You get errors because you are using symbolic references. If that is what you want, turn strictness off (at least in those blocks you are using them). Don't slab strictness on existing code if you don't know the implications. Don't dance to the pipes of strictness either - a programmer shouldn't be a slave of the language, at least not if the language is Perl.

    A way of solving this while using strict:

    my @interests = ("foo", "bar", "blah"); my %hash; foreach (@interest) { $hash {$_} = "My interest is $_"; } foreach (@interest) { print $hash {$_}, "\n"; }

    Abigail

      Thanks for your post.
      I wasnt so much interested in solving this problem, more so in the how/why it doesnt work.
      I'll try and do a bit more research on symbolic references.
      Cheers,
      Reagen
Re: use strict
by dragonchild (Archbishop) on Jun 01, 2004 at 12:59 UTC
    What you are attempting to do is called a symbolic or soft reference. There are hundreds of posts on Perlmonks as to why this is almost always not what you want to do. Basically, unless you know why you shouldn't do it, you shouldn't do it.

    While EdwardG provided you a solution based on aliasing, the proper solution you are looking for is a hash.

    my @interests = ( "foo", "bar", "blah" ); my %interest_expanded; foreach (@interests) { $interest_expanded{$_} = "My interest is $_"; } foreach (keys %interest_expanded) { print $_, " => ", $interest_expanded{$_} , "\n"; }

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: use strict
by gjb (Vicar) on Jun 01, 2004 at 13:01 UTC

    Why not consider using a hash? If I understand you correctly you want to assign to variables that have names you don't now in advance.

    use strict; my @interests = ( "foo", "bar", "blah"); my %hash; foreach (@interests) { $hash{$_} = "My interest is $_"; } foreach (keys %hash) { print "$hash{$_}\n"; }

    Hope this helps, -gjb-

    Update: Thanks dragonchild for pointing out a typo (%hash rather than @interests in the last foreach)

Re: use strict
by borisz (Canon) on Jun 01, 2004 at 13:00 UTC
    if you really need to do this,
    my @interests = ( "foo", "bar", "blah" ); foreach (@interests) { no strict 'refs'; $$_ = "My interest is $_"; } foreach (@interests) { no strict 'refs'; print $$_ , "\n"; }
    Boris
Re: use strict
by EdwardG (Vicar) on Jun 01, 2004 at 12:51 UTC
    use warnings; use strict; my @interests = ( "foo", "bar", "blah" ); $_ = "My interest is $_" for @interests; print $_,"\n" for @interests;