Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Good programming practice

by Fastolfe (Vicar)
on Nov 03, 2001 at 21:06 UTC ( [id://123053]=note: print w/replies, xml ) Need Help??


in reply to Good programming practice

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 ...

Replies are listed 'Best First'.
Re: Re: Good programming practice
by blakem (Monsignor) on Nov 04, 2001 at 12:58 UTC
    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

Re: Re: Good programming practice
by tretin (Friar) on Nov 04, 2001 at 10:39 UTC
    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).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found