http://www.perlmonks.org?node_id=875852

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


Why am I getting this error? This code has worked for me many times in the past. This is on ActiveState 5.10, but I tried it on 5.12 and got the same error.



use strict; use warnings; use Data::Dumper; use DBI; use DBD::ODBC; $|++; my $DSN = $ARGV[0]; my $UserName = $ARGV[1]; my $PassWord = $ARGV[2]; my $dbh = DBI->connect("dbi:ODBC:$DSN", $UserName, $PassWord, { R +aiseError => 1, AutoCommit => 1 }) or die "Couldn't connect to:\[$DSN +\] as \[$UserName\]: $DBI->errstr"; my @SELRow; my $SQL_Statement = " Select * From DBC.DBCInfo ; "; my $SELh = $dbh->prepare($SQL_Statement); $SELh->execute; while(@SELRow = $SELh->fetchrow_array) { print("$SELRow[0]:$SELRow[1]\n"); } $SELh->finish(); $dbh->disconnect(); print(Dumper(\@SELRow));


Solved!
changed: $DBI->errstr to $DBI::errstr Thanks AnonyMonk!

Replies are listed 'Best First'.
Re: Global symbol "$DBI" requires explicit package name
by Anonymous Monk on Dec 07, 2010 at 18:26 UTC
    Kinda doubt it
    use strict;
    use diagnostics;
    print "$DBI->errstr";
    print "$DBI->errstr";
    __END__
    Global symbol "$DBI" requires explicit package name at - line 3.
    Execution of - aborted due to compilation errors (#1)
        (F) You've said "use strict" or "use strict vars", which indicates
        that all variables must either be lexically scoped (using "my" or "state"),
        declared beforehand using "our", or explicitly qualified to say
        which package the global variable is in (using "::").
    
    
      I swear on my mother's grave (well she's not dead yet, but you get the picture) that I have been using this code for years without a problem. Did something change in DBI?

        This has nothing to do with DBI. You were using a variable you didn't declare ($DBI), and you asked to make such things errors by using (use strict;).

        While $DBI::errstr is also a variable you didn't declare, use strict; forgives qualified ("::") package variables.

        Although it's not documented, DBI->errstr might work. Maybe that's what you were using.