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

Re: Backdating strict

by dsheroh (Monsignor)
on Nov 18, 2020 at 08:42 UTC ( #11123775=note: print w/replies, xml ) Need Help??


in reply to Backdating strict

Lots of good answers already, but there's one point I didn't see anyone else making already:

strict is a lexical pragma.

The practical significance of this is that you don't have to slap use strict at the top of your old code and swallow the whole elephant in one gulp. As you're revisiting your code to maintain or debug it, you can add strict one sub, or even just one block, at a time, gradually increasing the proportion of the code which is running under strict - and also limiting how much of it is broken at a time as you are in the process of improving it.

Replies are listed 'Best First'.
Re^2: Backdating strict
by Bod (Chaplain) on Dec 06, 2020 at 21:40 UTC

    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?

      ... $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
Node Status?
node history
Node Type: note [id://11123775]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2021-04-17 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?