How to do conditional compile.

D

davids

I have a Perl/Tk program that uses the Win32::OLE modules to read
and write Excel spreadsheets. Actually, that is just an accessory
function. The purpose of the program is to take data from a set
of instruments.

Anyhow, the program includes statements like this:

1. use Win32::OLE::Const 'Microsoft Excel';

and later, buried in a subroutine, statements like:

2. $worksheet->PageSetup->{PaperSize} = xlPaperLetter;

The problem is at statement 1 - perl complains that

No type library matching "MicrosoftExcel" found ....

and at statement 2 -

Bareword 'xlPaperLetter' not allowed while 'strict subs' in use ...

and the program refuses to run. This happens on any computer that
Excel is not installed on.

My question is: What can I put in the code to cause it to not
use this Excel-only code if it doesn't have Excel installed.

My question is how can I detect whether Excel is installed at
run-time and skip compiling the subroutines containing these
statements based on it.

I realize that I can just comment them out and have two
versions of the program, but is there a clever way to do
it in Perl?

I've run into this problem in a different context where
I'd like to run the program on a computer that doesn't have
the instrument drivers installed - but I'd still like to
run it and use the data visualization features to review
measurement results.

Any hints appreciated.

Thanks,

Dave Smith
 
B

Brian McCauley

davids said:
I have a Perl/Tk program that uses the Win32::OLE modules to read
and write Excel spreadsheets. Actually, that is just an accessory
function. The purpose of the program is to take data from a set
of instruments.

Anyhow, the program includes statements like this:

1. use Win32::OLE::Const 'Microsoft Excel';
The problem is at statement 1 - perl complains that

No type library matching "MicrosoftExcel" found ....
and later, buried in a subroutine, statements like:

2. $worksheet->PageSetup->{PaperSize} = xlPaperLetter;

Bareword 'xlPaperLetter' not allowed while 'strict subs' in use ...

That's a compile time error because the function is not defined at
compile-time the compiler can't be sure it's a function.
and the program refuses to run. This happens on any computer that
Excel is not installed on.

You can move the problem to runtime by not omitting the () on the call
to the function xlPaperLetter thus making it explicit that it's a
function call.

$worksheet->PageSetup->{PaperSize} = xlPaperLetter();
My question is: What can I put in the code to cause it to not
use this Excel-only code if it doesn't have Excel installed.

Presumably it doesn't _use_ it. It is sufficient to simply make sure
it doesn't fail at compile time.
My question is how can I detect whether Excel is installed at
run-time and skip compiling the subroutines containing these
statements based on it.

Well you could use the fact that &xlPaperLetter exists.
exits(&xlPaperLetter)

But this doesn't do anything to suppress the annoying "No type
library" message. I can't immediately suggest what to do about that as
I am unfamiliar with Win32::OLE::Const.

To skip compilation of offending subroutines then the best way is to
place the them in another file and conditoinally require() it. However
thns
 
D

davids

Brian said:
To skip compilation of offending subroutines then the best way is to
place the them in another file and conditoinally require() it.

Thanks Brian. That's what I needed. Googling for
'perl "conditional require"' got me to some addiional hints. I moved
the Excel routines into a separate file named Excel_subs.pm and put the
following in my main routine:

my $HAS_EXCEL = eval "use Win32::OLE::Cont 'Microsoft Excel'";
if ($HAS_EXCEL) {require "Excel_subs"};

Then I use the variable $HAS_EXCEL to skip construction
of Tk menu items that offer to e.g. ship the data to
Excel.

At least that's the Idea, though I haven't got it fully tested
yet.

Thanks much for the help.

- David
 
B

Brian McCauley

davids said:
Thanks Brian. That's what I needed. Googling for
'perl "conditional require"' got me to some addiional hints. I moved
the Excel routines into a separate file named Excel_subs.pm and put the
following in my main routine:

my $HAS_EXCEL = eval "use Win32::OLE::Cont 'Microsoft Excel'";

I usually suggest doing

my $HAS_EXCEL = eval "use Win32::OLE::Cont 'Microsoft Excel'; 1 ";

or

my $HAS_EXCEL = eval {
require Win32::OLE::Cont;
Win32::OLE::Cont->import('Microsoft Excel');
1;
};

There explicit return value ensures that eval returns true on success
and false if there is an error.

But from what I read in your original post you were getting a warning
not an error and so $HAS_EXCEL will always be true even if you don't
have Excel.
 
N

nobull

davids wrote
Brian McCauley wrote

To skip compilation of offending subroutines then the best way is t
place the them in another file and conditoinally require() it

Thanks Brian. That's what I needed. Googling fo
'perl "conditional require"' got me to some addiiona hints. I move
the Excel routines into a separate file named Excel_subs.pm and pu th
following in my main routine

my $HAS_EXCEL = eval "use Win32::OLE::Cont 'Microsof Excel'"
I usually suggest doin

my $HAS_EXCEL = eval "use Win32::OLE::Cont 'Microsoft Excel';
"

o

my $HAS_EXCEL = eval
require Win32::OLE::Cont
Win32::OLE::Cont->import('Microsoft Excel')
1
}

There explicit return value ensures that eval returns true on succes
and false if there is an error

But from what I read in your original post you were getting a warnin
not an error and so $HAS_EXCEL will always be true even if you don'
have Excel
 
D

davids

Brian,

Thanks again. You are right - I needed to add the ";1" in the
eval. I found that my code didn't really cut it when I ran
it on another machine with Excel installed. It failed to load
the Excel modules, and while trying to figure the problem,
I discovered your post - which fixes the problem nicely.

Best wishes,

Dave Smith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top