For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2005 > IF statements and modules - do they mix?









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]

 

Author IF statements and modules - do they mix?
Dave Adams

2005-07-26, 5:00 pm

Does perl allow you to conditionally include a module?

For example:

#!/usr/bin/perl -w
use strict;
my $DEBUG =3D 0;
if (DEBUG) {
use diagnostics;
}
my $filename =3D "test$$.txt";
open (FH , ">$filename") || die "error: $!";
print (FH "hi");
close (FH);

Although this is a simple and silly example, it would be nice to be
able to include the diagnostic module by setting my debug to true.

Any Comments?
DA
John W. Krahn

2005-07-26, 5:00 pm

Dave Adams wrote:
> Does perl allow you to conditionally include a module?
>
> For example:
>
> #!/usr/bin/perl -w
> use strict;
> my $DEBUG = 0;
> if (DEBUG) {
> use diagnostics;
> }
> my $filename = "test$$.txt";
> open (FH , ">$filename") || die "error: $!";
> print (FH "hi");
> close (FH);
>
> Although this is a simple and silly example, it would be nice to be
> able to include the diagnostic module by setting my debug to true.


perldoc diagnostics

[snip]
Due to the interaction between runtime and compiletime issues,
and because it’s probably not a very good idea anyway, you may
not use "no diagnostics" to turn them off at compiletime.
However, you may control their behaviour at runtime using the
disable() and enable() methods to turn them off and on
^^^^^^^ ^^^^^^
respectively.



John
--
use Perl;
program
fulfillment
Charles K. Clarkson

2005-07-27, 4:00 am

Dave Adams <mailto:davidlamontadams@gmail.com> wrote:

: Does perl allow you to conditionally include a module?

In general, you can load a module at runtime by using
'require' and manually running its import() sub routine.

require Module;
Module::import( 'Import list' );

: For example:
:
: #!/usr/bin/perl -w
: use strict;
: my $DEBUG = 0;
: if (DEBUG) {
: use diagnostics;
: }

use constant DEBUG => 0;

if ( DEBUG ) {
require diagnostics;
diagnostics::import();
}

OR:

my $DEBUG = 0;

if ( $DEBUG ) {
require diagnostics;
diagnostics::import();
}


Though I think I like John Krahn's solution better.

use diagnostics;

my $DEBUG = 0;

diagnostics->disable() unless $DEBUG;


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328

Bob Showalter

2005-07-27, 5:02 pm

Charles K. Clarkson wrote:
> Dave Adams <mailto:davidlamontadams@gmail.com> wrote:
>
>
> In general, you can load a module at runtime by using
> 'require' and manually running its import() sub routine.
>
> require Module;
> Module::import( 'Import list' );


That should be

Module->import(@list);

Many (most?) modules do not implement an import() method, but simply rely on
the default behavior from Exporter.

Another option that can be used at runtime is:

eval "use Module(@list)";
Scott R. Godin

2005-07-27, 5:02 pm

Dave Adams wrote:
> Does perl allow you to conditionally include a module?
>
> For example:
>
> #!/usr/bin/perl -w
> use strict;
> my $DEBUG = 0;
> if (DEBUG) {
> use diagnostics;
> }
> my $filename = "test$$.txt";
> open (FH , ">$filename") || die "error: $!";
> print (FH "hi");
> close (FH);
>
> Although this is a simple and silly example, it would be nice to be
> able to include the diagnostic module by setting my debug to true.
>
> Any Comments?
> DA



If you're using Perl 5.7.3 or later you can

use if $DEBUG, diagnostics -verbose;

details in 'perldoc if'

:)

And all you people who answered with something other than this, aren't you
ashamed you didn't know about 'if' ? :)
John W. Krahn

2005-07-27, 5:02 pm

Scott R. Godin wrote:
> Dave Adams wrote:
>
> If you're using Perl 5.7.3 or later you can
>
> use if $DEBUG, diagnostics -verbose;
>
> details in 'perldoc if'
>
> :)
>
> And all you people who answered with something other than this, aren't
> you ashamed you didn't know about 'if' ? :)


Aren't you ashamed that you didn't read 'perldoc if' and see that the
correct syntax is:

use if $DEBUG, diagnostics => -verbose;

;-)

John
--
use Perl;
program
fulfillment
Scott R. Godin

2005-07-29, 5:01 pm

John W. Krahn wrote:
> Scott R. Godin wrote:
>
[snip][color=darkred]
>
> Aren't you ashamed that you didn't read 'perldoc if' and see that the
> correct syntax is:
>
> use if $DEBUG, diagnostics => -verbose;
>
> ;-)
>


LOL! got me =8)

That's what I get for snapping off a reply at the end of the day just before
heading home. :)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com