Getting script to run w/o optional module
Getting script to run w/o optional module
am 09.06.2011 23:42:55 von sono-io
I'd like to find a way to make a Perl script "modular". What I =
mean is that I want to be able to distribute a script either with or =
without a particular module/package/lib and have the main script run no =
matter if it's included or not.
What I'm trying to come up with is a "lite" version of a script =
that's missing some features. To "upgrade", all they'd have to do is =
drop in the new module without needing to change any code in the main =
script. Is that possible?
Using either require, use, or do produces an error if I run the =
script without the module. Is there a specific function for this =
purpose, or would I have to do something like wrap a 'use' or 'require' =
in an if statement? I've tried that and it doesn't work so maybe I'm =
way off base on that one.
I've been Googling for the last 2 days but not finding anything. =
I'm not really certain what to search for, so any pointers would be =
greatly appreciated.
Thanks,
Marc=
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Getting script to run w/o optional module
am 09.06.2011 23:46:38 von John SJ Anderson
On Thu, Jun 9, 2011 at 17:42, wrote:
> Â Â Â Â Using either require, use, or do produces an e=
rror if I run the script without the module. Â Is there a specific func=
tion for this purpose, or would I have to do something like wrap a 'use' or=
'require' in an if statement? Â I've tried that and it doesn't work so=
maybe I'm way off base on that one.
Something like:
my $module_is_enabled =3D 0;
eval 'use My::Special::Module';
$module_is_enabled =3D 1 unless $@;
should do what you're looking for.
j.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Getting script to run w/o optional module
am 10.06.2011 00:07:33 von Robert Wohlfarth
--90e6ba3fd8f7163e6504a54eae1e
Content-Type: text/plain; charset=ISO-8859-1
On Thu, Jun 9, 2011 at 4:42 PM, wrote:
> I'd like to find a way to make a Perl script "modular". What I mean
> is that I want to be able to distribute a script either with or without a
> particular module/package/lib and have the main script run no matter if it's
> included or not.
>
> What I'm trying to come up with is a "lite" version of a script
> that's missing some features. To "upgrade", all they'd have to do is drop
> in the new module without needing to change any code in the main script. Is
> that possible?
>
Would something like this meet your need? I found it on CPAN by
searchingfor "Module".
http://search.cpan.org/~ivorw/Module-Optional-0.03/lib/Modul e/Optional.pm
--
Robert Wohlfarth
--90e6ba3fd8f7163e6504a54eae1e--
Re: Getting script to run w/o optional module
am 10.06.2011 03:18:41 von sono-io
On Jun 9, 2011, at 2:46 PM, John SJ Anderson wrote:
> my $module_is_enabled =3D 0;
> eval 'use My::Special::Module';
> $module_is_enabled =3D 1 unless $@;
>=20
> should do what you're looking for.
John,
Thanks for the code. Between that and wrapping a call to an =
external sub in an if statement, it works well.
On Jun 9, 2011, at 3:07 PM, Robert Wohlfarth wrote:
> Would something like this meet your need?
>=20
> =
http://search.cpan.org/~ivorw/Module-Optional-0.03/lib/Modul e/Optional.pm
Robert,
That looks really interesting and may be exactly what I need. =
Thanks for sending the link.
Marc=
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Getting script to run w/o optional module
am 10.06.2011 14:01:37 von rvtol+usenet
On 2011-06-09 23:46, John SJ Anderson wrote:
> On Thu, Jun 9, 2011 at 17:42, wrote:
>> Using either require, use, or do produces an error if I run the script without the module. Is there a specific function for this purpose, or would I have to do something like wrap a 'use' or 'require' in an if statement? I've tried that and it doesn't work so maybe I'm way off base on that one.
>
> Something like:
>
> my $module_is_enabled = 0;
> eval 'use My::Special::Module';
> $module_is_enabled = 1 unless $@;
>
> should do what you're looking for.
It is wrong to test on $@ like that, even only because it is a global
variable.
In stead, you should test the return value of the eval.
For example like this:
perl -wle '
my @loaded;
my %error;
my @modules = qw/ My::Data::Dumper Data::Dumper His::Data::Dumper /;
for my $module ( @modules ) {
eval qq{use $module; push \@loaded, \$module; 1}
or do {
my $eval_error = $@ || "Zombie error!";
( $error{ $module } = $eval_error ) =~ s/ \(.*//s;
};
}
print "OK:", Dumper( \@loaded );
print "ERROR:", Dumper( \%error );
'
which prints:
OK:$VAR1 = [
'Data::Dumper'
];
ERROR:$VAR1 = {
'His::Data::Dumper' => 'Can\'t locate His/Data/Dumper.pm in @INC',
'My::Data::Dumper' => 'Can\'t locate My/Data/Dumper.pm in @INC'
};
And don't forget to check out the several plugin modules on CPAN!
See for example how Template::Plugin works.
--
Ruud
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/