Thanks for the varied suggestions. I'm still dealing with a few things
I cannot explain. I have provided below a very simple program that
produces errors in two scenarios, each case is resolved by reverting
back to the regular use statement.
#EXAMPLE1:
#!/usr/bin/perl -w
use strict;
my $GUI = 1;
use if $GUI,'Tk';
use if $GUI,'Win32Util';
my $top;
if ($GUI) {
$top = MainWindow->new();
Win32Util::maximize($top);
} else {
print "command line.\n";
}
#result
#Can't locate object method "new" via package "MainWindow" (perhaps you
forgot to load "MainWindow"?) at C:\DATA\SESS_KILL\test.pl line 12.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EXAMPLE 2:
#!/usr/bin/perl -w
use strict;
my $GUI = 1;
#use if $GUI,'Tk';
use Tk;
use if $GUI,'Win32Util';
my $top;
if ($GUI) {
$top = MainWindow->new();
Win32Util::maximize($top);
} else {
print "command line.\n";
}
#result
#Undefined subroutine &Win32Util::maximize called at
C:\DATA\SESS_KILL\test.pl line 14.
This error subsides when I do a use Win32Util.
Is there something I need to do differently when calling subroutines?
----- Original Message -----
From: $Bill Luebkert <[EMAIL PROTECTED]>
Date: Thursday, October 20, 2005 2:27 am
Subject: Re: [Perl-unix-users] ignoring conditional use module
statements
> [EMAIL PROTECTED] wrote:
> > How can you get perl to not check for modules that are loaded
> > conditionaly? In the example below perl would still error on
> the Tk
> > module(s) if they are not installed. I'm basically coding to
> support
> > both a GUI and command line mode, eventually across a couple of
> > platforms.
> >
> >
> > ~snip~
> >
> > $runmode eq "cmdline";
> >
> > if ($runmode eq "GUI") {
> > use Tk;
> > use Tk::HList;
> > use Tk::Text;
> >
> > }
> > use DBI;
> > use Term::ReadKey;
> > use Term::ReadLine;
> >
> > ~snip~
>
> There are about 3 ways to handle it.
>
> 1) use 'require' and maybe 'import' instead of use to delay loading.
>
> my $is_GUI = 1;
> ...
> require Tk;
> import Tk; # if needed
>
> 2) use 5.8 if conditional:
>
> my $is_GUI = 1;
> ...
> use if $is_GUI, 'Tk';
>
> 3) use an eval on the use to delay loading:
>
> my $is_GUI = 1;
> BEGIN { eval "use Tk" if $is_GUI; }
>
>
_______________________________________________
Perl-Unix-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs