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

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/"; require "incl/"; my $update_time = $dbh->selectrow_array("SELECT NOW()");

    The problem I am getting is Global symbol "$dbh" requires explicit package name at line 7. The variable $dbh is a database handle from DBD::mysql and it is defined in require "incl/"; 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 but that still gives Can't call method "selectrow_array" on an undefined value. As should be clear from the filename, 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/"; without the my keyword.

      Then it is a package-global variable. What namespace (package) does incl/ 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/ 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11123775]
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
Find Nodes?
    Voting Booth?

    No recent polls found