tags:

views:

117

answers:

3
my $now = &GetDate;
my $myHeader = &ReadMessage;
my $mySoftwareVersions = param('mySoftwareVersions');
my $q = new CGI;print $q->header();

use CGI::Carp(fatalsToBrowser);
getAllSoftwareVersions();

sub getAllSoftwareVersions
{
        my $user = "zxxx";
        my $passwd = "xxxx";
#       my $tableName = "config_table";
#       my $connection = DBI->connect("DBI:mysql:MESCI:hamysql02.stl.mo.boeing.com:18080", $user, $passwd, { AutoCommit => 0, RaiseError => 1}) or die "Couldn't connect to Database: " . DBI->errstr;
        print "Must be connected\n\n";
        print "\n\n";
# Error here.
        my @Rows = &getConfigTableRows($connection, $config_table, $mySoftwareVersions );
        my $total = @Rows;
        print "total is ";
        print $total;

The Above code dies with:

Global symbol "$connection" requires explicit package name

Edit This question is related to :

Hopefully the original poster will be able to clean this up so it makes more sense, but here's what we've got so far so we can attempt to help.

+2  A: 

Well, if this is the related content for this other question,

The reason this is erroring is because you have commented out the line that creates the connection variable.

How are you going to query the database for a table row when you don't have the database connection defined?

Kent Fredric
+2  A: 

Although Kent's answer is thinking ahead, the error is simply telling you that you did not declare $connection as a lexical ( "my" ) variable. So therefore, perl interprets it as that you must be referring to a package global.

Raw Perl does not complain when you use an undefined variable, it considers it a package global. You appear to have strict on somewhere (a standard and recommended practice), which wants you to declare your variables before using them. If you didn't declare the variable in the current package (or "namespace"), it assumes you're referring to a variable declared in another package, so it asks you to append the package name, just to keep everything clear and aboveboard.

Perl uses my to declare scoped variables, and our to declare package globals.

my $connection = "Rainbow";

OR

our $connection = 'French';


Just in case you got the wrong idea, the error message would go away if you turned strict off, your problem wouldn't. And they might go underground.

{ no strict;
  my @rows = getConfigTableRows( $nothing, @other_stuff ); 
}

Perl just won't complain that $nothing is nothing. And this easy-to-fix error could cause subtler errors in other places. Plus think if you had assigned $connection successfully, only to type:

{ no strict;
  my @rows = getConfigTableRows( $connecion, $config_table, $mySoftwareVersions );
}

Perl gives you a message about '$connecion' and hopefully, you can tell that it was a typo, and forgo at least 30 minutes not seeing it and wondering if your query is wrong, or whatever.

Axeman
Try rewording that, you make it sound like 'strict' is a bad thing.
Kent Fredric
I'm explaining the inference, I'm not endorsing anything. It helps to understand why Perl does not fail with "Undeclared variable" error--because in the superset of perl, undeclared variables aren't a compile error; they simply have a certain inference.
Axeman
yes, but still, the wording doesn't convey that. Instinctively the way its worded insinuates "oh, just turn off strict". I know its not *intentional* but that is how it reads.
Kent Fredric
+1  A: 

Re:

my $now = &GetDate;
my $myHeader = &ReadMessage;

If you are going to use & on your function calls, make sure you use parentheses too:

my $now = &GetDate();
my $myHeader = &ReadMessage();

Otherwise, the currently executing sub's parameters are made available to (and alterable by) the subroutine you are calling.

This means that if you use your cgi script under mod_perl, suddenly you are in effect doing

my $now = &GetDate( Apache2::RequestUtil->request );

which is likely to be very wrong if GetDate takes an optional argument.

ysth