Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Backdating strict

by Bod (Parson)
on Dec 06, 2020 at 21:40 UTC ( [id://11124756]=note: print w/replies, xml ) Need Help??


in reply to Re: Backdating strict
in thread Backdating strict

I've hit a problem rather early on with use strict;!

I am writing some code mostly to test a module I am writing for this purpose -> To Extend, to Use, to Create - of course, the new code has use strict; at the begining thanks to the good influence of the Monks...

#!/usr/bin/perl use strict; require "incl/common.pl"; require "incl/html.pl"; my $update_time = $dbh->selectrow_array("SELECT NOW()");

The problem I am getting is Global symbol "$dbh" requires explicit package name at eric.pl line 7. The variable $dbh is a database handle from DBD::mysql and it is defined in require "incl/common.pl"; without the my keyword.

I have tried adding my $dbh; before the require statement thinking that I could simply declare the variable before assigning it within common.pl but that still gives Can't call method "selectrow_array" on an undefined value. As should be clear from the filename, common.pl is used by every script on the website so changing the way the database handle is declared and defined is not something I would want to do lightly...although I doubt anything untoward would happen I would rather know.

Any thoughts on how I should proceed?

Replies are listed 'Best First'.
Re^3: Backdating strict
by AnomalousMonk (Archbishop) on Dec 06, 2020 at 23:45 UTC
    ... $dbh ... is defined in require "incl/common.pl"; without the my keyword.

    Then it is a package-global variable. What namespace (package) does incl/common.pl use? If there is no explicit package statement in the required file, I believe it will be in the default main package (update: or whatever package is active in the file into which incl/common.pl is required). So, try
        my $update_time = $main::dbh->selectrow_array("SELECT NOW()");
    i.e., use the fully qualified variable name.

    Also, $::dbh is shorthand for $main::dbh.


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124756]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-25 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found