Home > Archive > PERL Beginners > April 2005 > Require / Use
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Paul Kraus 2005-04-21, 3:56 pm |
| Why would one use Require instead of Use?
| |
| Offer Kaye 2005-04-21, 3:56 pm |
| On 4/21/05, Paul Kraus wrote:
> Why would one use Require instead of Use?
>=20
Because you want the action at run-time (require) vs. compile time
(use), is the usual reason, I would guess.
--=20
Offer Kaye
| |
| Manav Mathur 2005-04-21, 3:56 pm |
|
Additionally, the import method defined in a package(or inhertied by it) is
not called when you 'require' a module. So, even @EXPORT symbols in the
'require'd module (which are by default imported in the current namespace
when you 'use' it) are not imported.
See perldoc perlmod for more on this.
Manav
|-----Original Message-----
|From: Offer Kaye [mailto:offer.kaye@gmail.com]
|Sent: Thursday, April 21, 2005 6:36 PM
|To: Perl Beginners
|Subject: Re: Require / Use
|
|
|On 4/21/05, Paul Kraus wrote:
|> Why would one use Require instead of Use?
|>
|
|Because you want the action at run-time (require) vs. compile time
|(use), is the usual reason, I would guess.
|
|--
|Offer Kaye
|
|--
|To unsubscribe, e-mail: beginners-unsubscribe@perl.org
|For additional commands, e-mail: beginners-help@perl.org
|<http://learn.perl.org/> <http://learn.perl.org/first-response>
|
|
|
****************************************
*****************
Disclaimer:
The contents of this E-mail (including the contents of the enclosure(s) or attachment(s) if any) are privileged and confidential material of MBT and should not be disclosed to, used by or copied in any manner by anyone other than the intended addressee(s)
. In case you are not the desired addressee, you should delete this message and/or re-direct it to the sender. The views expressed in this E-mail message (including the enclosure(s) or attachment(s) if any) are those of the individual sender, except wh
ere the sender expressly, and with authority, states them to be the views of MBT.
This e-mail message including attachment/(s), if any, is believed to be free of any virus. However, it is the responsibility of the recipient to ensure that it is virus free and MBT is not responsible for any loss or damage arising in any way from its us
e
****************************************
*****************
| |
| Paul Kraus 2005-04-21, 3:56 pm |
| > Because you want the action at run-time (require) vs. compile time
> (use), is the usual reason, I would guess.
Not to sound daft but why would you want to do that? What would be
gained or lossed. What would be an advantage of using require?
Paul
| |
| Chris Devers 2005-04-21, 3:56 pm |
| On Thu, 21 Apr 2005, Paul Kraus wrote:
> Not to sound daft but why would you want to do that? What would be
> gained or lossed. What would be an advantage of using require?
Normally, you wouldn't.
Think of 'require' as a vestigal organ from Perl4 that has little or no
purpose today with Perl5.
For the most part, it's safe to pretend that 'require' doesn't exist.
--
Chris Devers
| |
| Paul Kraus 2005-04-21, 3:56 pm |
| > Think of 'require' as a vestigal organ from Perl4 that has little or no
> purpose today with Perl5.
>=20
> For the most part, it's safe to pretend that 'require' doesn't exist.
Perfect. Just wanted to make sure I wasn't missing something.
| |
| Peter Scott 2005-04-21, 3:56 pm |
| On Thu, 21 Apr 2005 09:39:03 -0400, Chris Devers wrote:
> On Thu, 21 Apr 2005, Paul Kraus wrote:
>
>
> Normally, you wouldn't.
>
> Think of 'require' as a vestigal organ from Perl4 that has little or no
> purpose today with Perl5.
>
> For the most part, it's safe to pretend that 'require' doesn't exist.
Ahem. It is most useful when I want to determine the module to load
dynamically, or when I want to load a module only if necessary to save the
time spent compiling it in the cases when it is not needed.
--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
| |
| Manav Mathur 2005-04-21, 3:56 pm |
|
perldoc perlmod also goes to say
<quote>
An exception would be if two modules each tried to "use" each other, and
each also called a function from that other module. In that case, it's easy
to use "require"s instead.
</quote>
anyone knows how/why require is useful in this case ??
|-----Original Message-----
|From: Peter Scott [mailto:Peter@PSDT.com]
|Sent: Thursday, April 21, 2005 7:22 PM
|To: beginners@perl.org
|Subject: Re: Require / Use
|
|
|On Thu, 21 Apr 2005 09:39:03 -0400, Chris Devers wrote:
|
|> On Thu, 21 Apr 2005, Paul Kraus wrote:
|>
|>> Not to sound daft but why would you want to do that? What would be
|>> gained or lossed. What would be an advantage of using require?
|>
|> Normally, you wouldn't.
|>
|> Think of 'require' as a vestigal organ from Perl4 that has little or no
|> purpose today with Perl5.
|>
|> For the most part, it's safe to pretend that 'require' doesn't exist.
|
|Ahem. It is most useful when I want to determine the module to load
|dynamically, or when I want to load a module only if necessary to save the
|time spent compiling it in the cases when it is not needed.
|
|--
|Peter Scott
|http://www.perlmedic.com/
|http://www.perldebugged.com/
|
|
|--
|To unsubscribe, e-mail: beginners-unsubscribe@perl.org
|For additional commands, e-mail: beginners-help@perl.org
|<http://learn.perl.org/> <http://learn.perl.org/first-response>
|
|
****************************************
*****************
Disclaimer:
The contents of this E-mail (including the contents of the enclosure(s) or attachment(s) if any) are privileged and confidential material of MBT and should not be disclosed to, used by or copied in any manner by anyone other than the intended addressee(s)
. In case you are not the desired addressee, you should delete this message and/or re-direct it to the sender. The views expressed in this E-mail message (including the enclosure(s) or attachment(s) if any) are those of the individual sender, except wh
ere the sender expressly, and with authority, states them to be the views of MBT.
This e-mail message including attachment/(s), if any, is believed to be free of any virus. However, it is the responsibility of the recipient to ensure that it is virus free and MBT is not responsible for any loss or damage arising in any way from its us
e
****************************************
*****************
| |
| Chris Devers 2005-04-21, 3:56 pm |
| On Thu, 21 Apr 2005, Peter Scott wrote:
> Ahem. It is most useful when I want to determine the module to load
> dynamically, or when I want to load a module only if necessary to save
> the time spent compiling it in the cases when it is not needed.
:-)
I think that counts as an esoteric-enough requirement that beginners can
safely ignore the need for it unless & until they hit that wall.
I'm assuming that most beginners won't be writing much or any code that
needs to optimize loading a module multiple times...
--
Chris Devers
| |
| Charles K. Clarkson 2005-04-21, 3:56 pm |
| Chris Devers <mailto:cdevers@pobox.com> wrote:
: : Ahem. It is most useful when I want to determine the module to load
: : dynamically, or when I want to load a module only if necessary to
: : save the time spent compiling it in the cases when it is not needed.
:
: :-)
:
: I think that counts as an esoteric-enough requirement that beginners
: can safely ignore the need for it unless & until they hit that wall.
:
: I'm assuming that most beginners won't be writing much or any code
: that needs to optimize loading a module multiple times...
Dynamic loading does not apply just to multiple loading of a single
module. It could also be used to check the existence of a module before
loading it.
An answer to the original question is incomplete without at least
a mention of the usefulness of 'require' under some intermediate to
advanced perl programming.
my $mail_module = load_mail_module();
warn q(No mail module) unless $mail_module;
sub load_mail_module {
eval { require mail_foo };
unless ( $@ ) {
mail_foo::import();
return 'mail_foo';
}
eval { require mail_bar };
unless ( $@ ) {
mail_bar::import();
return 'mail_bar';
}
eval { require mail_baz };
unless ( $@ ) {
mail_baz::import();
return 'mail_baz';
}
return;
}
... or to load modules only as needed.
print redirect( '/' );
sub user_data_form {
require Data::FormValidator;
Data::FormValidator::import();
# ...
}
sub redirect {
my $page = shift;
require CGI;
return CGI::redirect( $page );
}
__END__
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
| |
| Randal L. Schwartz 2005-04-21, 3:56 pm |
| >>>>> "Paul" == Paul Kraus <paul.kraus@gmail.com> writes:
[color=darkred]
Paul> Not to sound daft but why would you want to do that? What would be
Paul> gained or lossed. What would be an advantage of using require?
if ($certain_condition) {
require Expensive::Module;
my $object = Expensive::Module->new(blah);
...
}
No point in loading Expensive::Module if $certain_condition isn't
true. That's hard to do with "use" instead of require.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
| |
| mgoland@optonline.net 2005-04-21, 3:56 pm |
|
----- Original Message -----
From: merlyn@stonehenge.com (Randal L. Schwartz)
Date: Thursday, April 21, 2005 11:39 am
Subject: Re: Require / Use
>
> Paul> Not to sound daft but why would you want to do that? What
> would be
> Paul> gained or lossed. What would be an advantage of using require?
>
>
> if ($certain_condition) {
> require Expensive::Module;
> my $object = Expensive::Module->new(blah);
> ...
> }
>
> No point in loading Expensive::Module if $certain_condition isn't
> true. That's hard to do with "use" instead of require.
Hmm, I find your comment interesting. Can you explain "Under the cover" difference between or give a small insight:
if ($certain_condition) {
require Expensive::Module;
my $object = Expensive::Module->new(blah);
...
}
and
if ($certain_condition) {
use Expensive::Module;
my $object = Expensive::Module->new(blah);
...
}
??
>
> --
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503
> 777 0095
> <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and open-enrollment
> Perl training!
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
| |
| Randal L. Schwartz 2005-04-21, 3:56 pm |
| >>>>> "mgoland" == mgoland <mgoland@optonline.net> writes:
mgoland> if ($certain_condition) {
mgoland> require Expensive::Module;
mgoland> my $object = Expensive::Module->new(blah);
mgoland> ...
mgoland> }
mgoland> and
This require happens only when we reach that line of code at runtime.
mgoland> if ($certain_condition) {
mgoland> use Expensive::Module;
mgoland> my $object = Expensive::Module->new(blah);
mgoland> ...
mgoland> }
This "use" happens when the code is compiled... That is, *always*.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
| |
| mgoland@optonline.net 2005-04-21, 3:56 pm |
|
----- Original Message -----
From: merlyn@stonehenge.com (Randal L. Schwartz)
Date: Thursday, April 21, 2005 12:10 pm
Subject: Re: Require / Use
>
> mgoland> if ($certain_condition) {
> mgoland> require Expensive::Module;
> mgoland> my $object = Expensive::Module->new(blah);
> mgoland> ...
> mgoland> }
> mgoland> and
>
> This require happens only when we reach that line of code at runtime.
>
> mgoland> if ($certain_condition) {
> mgoland> use Expensive::Module;
> mgoland> my $object = Expensive::Module->new(blah);
> mgoland> ...
> mgoland> }
>
> This "use" happens when the code is compiled... That is, *always*.
That is a bit of counter-intuitive for me, but a good learning lesson [ now having the fact, realising some odd errors ]. Is there a specific reason ??
>
> --
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503
> 777 0095
> <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and open-enrollment
> Perl training!
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
| |
|
|
|
|
|