How to do conditional compile.

Discussion in 'Perl Misc' started by davids, Apr 1, 2006.

  1. davids

    davids Guest

    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
     
    davids, Apr 1, 2006
    #1
    1. Advertising

  2. davids wrote:
    > 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
     
    Brian McCauley, Apr 1, 2006
    #2
    1. Advertising

  3. davids

    davids Guest

    Brian McCauley wrote:

    > 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
     
    davids, Apr 1, 2006
    #3
  4. davids wrote:
    > Brian McCauley wrote:
    >
    > > 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'";


    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.
     
    Brian McCauley, Apr 2, 2006
    #4
  5. davids

    Guest

    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
     
    , Apr 2, 2006
    #5
  6. davids

    davids Guest

    Brian McCauley wrote:

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

    >>

    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

    >> 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.
     
    davids, Apr 8, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Miller

    Conditional compile in VHDL

    Dave Miller, Jan 30, 2005, in forum: VHDL
    Replies:
    5
    Views:
    8,387
    Thomas Entner
    Feb 9, 2005
  2. Brian Edginton

    Conditional Compile

    Brian Edginton, Jul 8, 2003, in forum: Java
    Replies:
    35
    Views:
    4,112
    Jim Sculley
    Jul 13, 2003
  3. Alec S.
    Replies:
    10
    Views:
    10,172
    Alec S.
    Apr 16, 2005
  4. Nagaraj
    Replies:
    1
    Views:
    879
    Lionel B
    Mar 1, 2007
  5. timjowers
    Replies:
    7
    Views:
    4,355
    heyjude
    Feb 2, 2011
Loading...

Share This Page