• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

PERL and DBI Programming, I cannot create & assign new STRING Variable within $sth->fetch()???

My partial coding as followed  and I keeps getting compilation errors,  Please let me know why I cannot create and assign a new string variable:


use strict;
use DBI;

my $dbh = DBI->connect ('dbi:Oracle:temp', 'db', 'dboracle',
                      {RaiseError => 1, AutoCommit => 0 } );
#create SQL
my $sql = qq { SELECT userlabel, lineCoding, Attenuation from T1X};

#prepare the SQL execute
my($userlable,$linecoding, $attenuation);
my $sth = $dbh->prepare ( $sql );
$sth->bind_columns(undef,\$userlable, \$linecoding, \$attenuation);

while($sth->fetch() )
   if ($linecoding eq 0) { $new= 'AMI' }
    print "$new \t" ;
  print "Userlable=$userlable \t LineCoding=$linecoding \t Attenuation=$attenuation\n"

Here is the error message:
perl oracle.pl
Global symbol "$new" requires explicit package name at oracle.pl line 38.
Global symbol "$new" requires explicit package name at oracle.pl line 40.
Execution of oracle.pl aborted due to compilation errors.

Please let me know how I can assign a string to a new variable after I evaluate the result from my Retrieval SQL!!!
  • 2
1 Solution

Because you are using the strict pragma, you must declare all of your variables. You have declared several veriables using my(); However, you never declared the variable $new. Also, because you are only setting the value if the match is made you need to declare the variable before the if statement.

my $new;
if ($linecoding eq 0) { $new= 'AMI' }
wongglaAuthor Commented:
THANKS,  I am new to PERL programming,   What is a Strict pragma?  Is there any good book that you can recommand me to use?

THANKS so much,  it works!!!
This is a link to the Perl documentation for strict from ActivePerl, only because perldoc.com is not coming up for me.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now