Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Good programming practice

by Purdy (Hermit)
on Nov 03, 2001 at 20:48 UTC ( #123049=perlquestion: print w/replies, xml ) Need Help??

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

Seeking some advice here. I finally got my centralized script to work, but I cannot get it to work with 'use strict' and I get warnings with '-w'. Whenever I use my central script, the script that uses it, looks something like:

$database = "dbname"; require '/home/sites/jdb.cgi'; # now I can do whatever with $dbh

So when I slap in the '-w' (err, really I start with it - no honestly! :)), I get the following errors:

Name "main::dbh" used only once: possible typo at ./sqltest.pl line 8.
Name "main::database" used only once: possible typo at ./sqltest.pl line 5.

And if I 'use strict', I get:

Global symbol "$database" requires explicit package name at ./sqltest.pl line 5.
Global symbol "$dbh" requires explicit package name at ./sqltest.pl line 8.
Execution of ./sqltest.pl aborted due to compilation errors.

I put in a 'main::' in front of the variable names and that does fix it (not the warnings), but is that the solution? Putting 'main::' in front of everything?

I've heard of this 'our' declaration, but I don't have the latest version of Perl to have that (but if that's the answer, I'd still like to know).

If you check out the linked node, you'll see why I have/want this centralized script and the reason I designed it that way. But I don't like not being able to use the '-w' and 'use strict' ... am I doomed to have it only one way or another? Or is there a better way?

My next move is to read through the perldoc on the strict pragma. I guess I don't really understand that. But I'd still like to know if there's a better way to design such a system. Thanks in advance for the enlightenment! I look forward to learning an elegant solution. :)

Jason

Replies are listed 'Best First'.
Re: Good programming practice
by Fastolfe (Vicar) on Nov 03, 2001 at 21:06 UTC
    The warnings are just pointing you at a potential problem. It's only seeing those variables used in your script once, which generally means you're setting them and not using them, or using them without setting them, that sort of thing.

    If you know what you're doing, and setting them once and using them via a script that you're requireing, then this warning is spurious and you need a way to quell the warning. The easiest way is just to mention it a second time, in a no-op fashion:

    $used_once = "some value"; $used_once; # void context, does nothing
    Arguably changing your required code into a proper module would force you into a syntax that's more compatible with this warning, but the workaround above should help you for now. A possible syntax I wouldn't be surprised coming out of this:
    use EmeraldWarp::DBI; # instead of requiring it our $database = "whatever"; ... # the actual "work" is placed in a 'new' subroutine # instead of being executed as part of the loading of # the module my $dbh = new EmeraldWarp::DBI($database); # or just "whatever" and o +mit $database entirely ...
      In recent version of perl (5.6.0 and above) you can suppress this warning using the lexically scoped:
      no warnings "once";
      The following script will complain about tigers, but not lions...
      #!/usr/bin/perl -wT { no warnings "once"; $lions = 123; } $tigers = 456; print "Hello world\n";
      The full list of default categories can be found at perllexwarn.

      -Blake

      That's great! I've noticed that more than once when writing my scripts and using -w and, or, use strict, and now I know why. Now those warnings and failed scripts make a whole lot more sense. Thanks for asking a good question!

      just my 5 cents (ran out of pennies!)
        The intent behind only used once warnings is that it may be a mistyped variable - for instance, you might have a $variable in your code which somewhere is referred to as $vairable. This warning would catch it (however it seems a bit pointless to be warned of single-use variables when at the same type strict will force an error at compile time on mistyped variable names).
Re: Good programming practice
by wog (Curate) on Nov 03, 2001 at 20:53 UTC
    While the our declartion is only supported in recent perls, you can use use vars to perform a similar task. (Note that use vars and our is not indentical. Use vars is package-scoped (you do it and it works for any use of that package); our is lexically scoped like my.)
      Sweet - that answers one part of my question. Didn't know about use vars.

      I'm still interested in knowing if this is the best design for such a system.

      Jason

      wog - I ran out of votes, but there's tomorrow!

        i do have a problem with require - it's a bit old, and more importantly, it's not portable. If you design a CPAN style module, you can install it on any system and access it from any program. Then you can 'use' the much better:use jdb; instead of require.

        A great reference for building CPAN style modules can be found here: perlnewmod (and no, you don't have to submit your module to CPAN for it to be a CPAN style module).

        But first, read perlmod. :)

        jeffa

Re: Good programming practice
by dragonchild (Archbishop) on Nov 05, 2001 at 21:11 UTC
    Use my. Learn about my. Embrace my. Scoping is your issue, I think.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://123049]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2019-12-15 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?