develooper Front page | perl.perl5.porters | Postings from October 2009

Re: [perl #70067] Variable Suicide Involving Dispatch Tables

Thread Previous
From:
John Tobin
Date:
October 28, 2009 06:05
Subject:
Re: [perl #70067] Variable Suicide Involving Dispatch Tables
Message ID:
20091028130509.GR9655@scss.tcd.ie
On Tue, Oct 27, 2009 at 08:47:58PM -0700, David Yingling wrote:
> The problem is that some of the variables I have declared "disappear" they lose their definition and their value.  These variables are %macros, $fake_hash, and @macros.  $fake_hash and @macros are my poor attempts at a workaround using  different data types.  My thinking was the problem was with hashes and not all kinds of variables; however, none of those workarounds worked.  I left them in the example test code below in case you would want to test them.
> 
> Below are 2 programs.  The first one (perlbug.pl) includes the code that causes the "variable suicide" as demonstrated by its output and the warnings about using undefined variables, which if you look at the code have both been defined and are still in scope.  The second one (perlworkaround.pl) includes a workaround that fixes the problem and demonstrates what the output "should" look like 

I may not be answering your question, but I have identified two problems
for you.

>             #delete macro char $ that might conflict with per's sigils.
>             $macro =~ s/\$//;

These are lines 52 and 53 of perlbug.pl.  You strip the $ from the macro
name, so a line like
    $foo = bar
will be stored in %macros as
    $macros{foo} = "bar"

>                 #Regex capture out the $macro name from the line, and so I can check its length().
>                 $$line =~ /(\$\w+\b)/; #Macros are letters, numbers and _'s like perl identifiers.
>                 my $macro = $1; 

Lines 82-84 of perlbug.pl; you capture a macro, including the leading $.

>                 #Use substr to remove $macro and replace it with the macro's value, $macros{$macro}.
>                 substr ($$line, $index, $length) = $macros{$macro};
>                 print "REPLACED macro [$macro] with macro value [$macros{$macro}]: [$$line]\n";

Lines 105-107 of perlbug.pl; you lookup $macros{$macro}, where $macro
includes the leading $.  If the macro used is $foo, you lookup
    $macros{'$foo'}
You should strip the $ from the macro name in both places, or neither
place.

>     LINE: for (my $z = 1; $z <= $#$lines; $z++) {

Line 171 of perlbug.pl; the second time that parse_config() is called,
lots of lines will be undef.  This loop should skip those lines, with:
    if (not defined $lines->[$z]) {
        next LINE;
    }
This change will solve the remaining "Use of uninitialized value"
warnings.

If you make those changes does your program run as you expect?

-- 
John Tobin
"For a while, I've had a few "undeletable Outlook folders". Even after
deleting all the messages from them, Outlook just complains when I try
to delete them."
    -- http://blogs.msdn.com/oldnewthing/archive/2005/10/17/481810.aspx
       Raymond Chen, Microsoft developer, has Outlook problems too.

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About