in reply to Re: Preventing used modules from leaking to main script
in thread Preventing used modules from leaking to main script

The use IO::Socket::INET; should be in the main if main is going to call it!

And that's exactly what I want to enforce.

Perhaps I didn't make myself clear enough. What I'm saying is that there is apparently no way right now to make sure that your script/module has a use (or require) statement for every module it's using.

It's happened to me on multiple occasions that I forgot to add a use statement (most often when adding the very first Data::Dumper debug message in a script), and instead of breaking right away the script would work fine until I removed an unrelated module which did happen to use Data::Dumper.

(Of course you can very well make the argument that not checking to make sure you've included all needed modules is sloppy coding.)

Replies are listed 'Best First'.
Re^3: Preventing used modules from leaking to main script
by Hue-Bond (Priest) on Sep 20, 2006 at 17:02 UTC
    And that's exactly what I want to enforce.

    You get that behaviour out of the box:

    { package FOO; use Data::Dumper; print Dumper \"bar"; } print Dumper \"baz"; __END__ Name "main::Dumper" used only once: possible typo at - line 7. $VAR1 = \'bar'; print() on unopened filehandle Dumper at - line 7.

    However, if you call Dumper by its fully qualified name (ie Data::Dumper::Dumper, or IO::Socket::INET->new in your example), there's no way to prevent it from working:

    { package FOO; use Data::Dumper; print Dumper \"bar"; } no Data::Dumper; ## not even this print Data::Dumper::Dumper \"baz"; __END__ $VAR1 = \'bar'; $VAR1 = \'baz';

    --
    David Serrano

Re^3: Preventing used modules from leaking to main script
by jdtoronto (Prior) on Sep 20, 2006 at 17:55 UTC
    And indeed I would make that very point. You should check, because when an error like that occurs that is how you know you have left something out! You can catch some things, you can save in having to use Data::Dumper; everywhere by creating a debug method in your base class which prints the Dumper method, inherit that in every module and then you never need to use Data::Dumper; again! It also means you can use a global to turn the debug printing on and off. That's what I do, I have a $DEBUG variable at the top of the code, it will turn the dumping on or off.

    But sometimes you just gotta face up to the fact that you are human and deal with it! I do, every day!!

    jdtornto