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

Hi all,

I am a newbie trying to use the Perl for all kinds of Text Processing

Many a times, i come across the need for using the matched memory variable ($1, $2, $3, ...) dynamically. i.e. ${$i} where $i = 1,2,3,...
This doesnt works when i use the strict package, i.e. use strict;

Please explain me abt the problem that i am doing while using strict

Thanx in advance

Cheers

- Sampath

  • Comment on how to dynamically declare a varialble?

Replies are listed 'Best First'.
Re: how to dynamically declare a varialble?
by BUU (Prior) on May 22, 2004 at 04:47 UTC
    It doesn't work because thats one of the main points of strict, you can't use symbolic references. If you want to use those references, turn off strict, but don't get mad if people yell at you.

    I have no idea what your actual code looks like, but you might want to realize that m// will return a list of variables it captures in parens if you call it in list context, which will remove the need for symbolically accessing the capture variables. Example:
    #badway /(foo)(bar)(baZ)/; if( $bar ) { return ${$bar}; }
    (Contrived of course..)
    #better way use strict; @matches = /(foo)(bar)(baz)/; if( $bar ) { return $matches[$bar]; }
Re: how to dynamically declare a varialble?
by Fletch (Bishop) on May 22, 2004 at 12:52 UTC

    See also perldoc perlvar for @- and @+ which can be used (with substr) to fetch out the nth matched subpattern (the entry for @- has the recipie).

Re: how to dynamically declare a varialble?
by punkish (Priest) on May 22, 2004 at 19:04 UTC
    As others have advised, use strict doesn't allow symbolic references. Per good coding practices, symbolic references are generally ill-advised as they can lead to various kinds of problems that use strict is meant to avoid in the first place.

    That said, there might be legitimate occassions where you might want use strict and still want some symbolic references. You can do that like so --

    use strict; no strict 'refs';

    Good luck.

Re: how to dynamically declare a varialble?
by gmpassos (Priest) on May 22, 2004 at 23:34 UTC
    strict is not just about variables, but also for subroutines and references.

    Personally I like to use strict only for variables since I like to play with references and the symbol table, soo, I always use:

    use strict qw(vars);

    Graciliano M. P.
    "Creativity is the expression of the liberty".

Re: how to dynamically declare a varialble?
by dragonchild (Archbishop) on May 22, 2004 at 15:41 UTC
    You want to use a hash for this.

    Update: Jenda pointed me to the actual question.

    Sampath - you want to assign the matches to an array. Something along the lines of:

    my $string = "aabbccdd"; if (my @matches = $string =~ /(aa)..(cc)../) { print "@matches\n"; print "$matches[0] ... $matches[1]\n"; }

    The difference is that $1 will be in $matches[0], and so forth.

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

      No he does not. Looks like someone read just the node subject and not the text :-)

      If he was trying to declare (well actually USE) variables with ad hoc names, you'd be right, but he just needs to access the regexp matches. I think in this case the easiest solution is to turn the strict refs off in the block. Or rewrite the code so that he gets the matches as a list. Which is not always possible without huge changes to the code.

      Jenda
      Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
         -- Rick Osborne

      Edit by castaway: Closed small tag in signature

        use strict; my %hash = ( 1 => \$1, 2 => \$2 ); my $hash = 1; "aa bb cc" =~ /(aa)/ and print ${ $hash{$hash} },$/; __END__ aa
        Ahhh. I missed the phrase "matched memory variable". I will reply again.

        Your solution is poor - much better is to assign the matches to an array.

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