Devel::UseFromCommandLineOnly - use a module from the command line only
package Foo; use base qw(Devel::UseFromCommandLineOnly); # it's okay to use from the command line # these examples will work perl -MFoo -E 'say "This will work!"' perl -MFoo -E 'say "This will work!"' echo 'use Foo; use 5.010; say "This will work!' | perl # but not from a file or module # these examples will die echo "use Foo;" > /tmp/foo.pl; perl /tmp/foo.pl echo "package Bar; use Foo;" > /tmp/Bar.pm; perl -I/tmp -MBar
This module prevents you from loading any subclass of it from anywhere but the command line.
This is most useful for writing development tools that monkeypatch other people's code. These hacks are fine to enable from the command line during development, but you wouldn't want to allow anyone to perminatly install them in any code that they could ship as the hacks could break at any point. See Test::EnhancedIs as a good example of this.
To use it you simply subclass the module:
package Foo; use base qw(Devel::UseFromCommandLineOnly);
This exposes an
import routine that checks if you're calling it from a script or module or from the command line and throws an exception if it's the former.
The one place that subclasses of this module will be needed to be loaded from within a script that is testing that subclass. In this case it's possible to override this module's behavior:
#!/usr/bin/perl use Test::More tests => 1; use Foo qw(disable_command_line_checks); isa_ok(Foo->new(), "Foo");
End users should NEVER EVER DO THIS. Or, if they do, they're playing with fire and deserve to get burnt...
Written by Mark Fowler <email@example.com>
Copryright Mark Fowler 2009. All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Please see http://www.twoshortplanks.com/project/devel-usefromcommandline for details of how to submit bugs, access the source control for this project, and contact the author.