[kamloops-pm] Hello
Bradley K. Embree
bke at bkecc.com
Thu Oct 9 14:05:16 CDT 2003
> -----Original Message-----
> From: kamloops-pm-bounces at mail.pm.org
> [mailto:kamloops-pm-bounces at mail.pm.org]On Behalf Of Anthonyj
> Sent: Thursday, October 09, 2003 11:29 AM
> To: kamloops-pm at mail.pm.org
> Subject: Re: [kamloops-pm] Hello
>
>
> Well, here's one to discuss. I'm still fairly new to perl, and I ran into
> an odd problem.
>
<snip>
>
> sub display_goal_posts
> {
>
> #get discussion for current goal
> our ($sth, $SQL);
>
> $SQL="SELECT * FROM goal_posts WHERE Project = '1' and Goal = '2'";
> $sth=getRS($SQL, $dbh);
>
> my %goal_posts;
> while (my $row = $sth->fetchrow_hashref() )
> {
> $goal_posts{ID} = $row->{ID};
> $goal_posts{Goal} = $row->{Goal};
> $goal_posts{Text} = $row->{Text};
>
> open (HTML,"<" . $goal_post) or print "Cannot open $goal_post\n";
> my @table_template = <HTML>;
> close (HTML);
>
> my %replace;
> $replace{POST_TEXT} = $goal_posts{Text};
>
> my $post_data .= join("\n", @table_template);
> $post_data =~ s|<!--%(.*?)%-->|$replace{$1}|gs;
> }
>
> #print $post_data;
> return $post_data;
>
> }
>
This is a scoping issue.
Short answer:
Declare $post_data before entering the while loop. I.E.:
my %goal_posts;
while (my $row = $sth->fetchrow_hashref() )
becomes
my %goal_posts;
my $post_data;
while (my $row = $sth->fetchrow_hashref() )
Longer answer:
put
use strict;
somewhere in the code it will help catch these types of errors (but you may
find your code needs a lot of rewriting to get it working under strict).
For a detailed explanation of scoping see:
http://perl.plover.com/FAQs/Namespaces.html
For a quick script to play with try this:
#use strict;
my @array = ( 1 .. 10 );
# my $i;
while ( my $i = shift @array ) {
print "$i\n";
}
print "$i\n";
Run it and look at the output. Then uncomment use strict; and try to run it.
Then uncomment my $i and run it again. For even more fun change my $1; to my
$i = 42; and run again. Compare the output. And then change while ( my $i =
... to while ( $i = ... and see the difference.
Bradley K. Embree, IT Support
Excel Kitchens
Email: bkembree at excelkitchens.ca
Phone: 1.250.376.8713
Fax: 1.250.376.4511
More information about the kamloops-pm
mailing list