Re: Running the same perl script on Win32 and Unix



Mahdi A Sbeih wrote:
> Hi there,
>
> I have a perl script that will run on both Unix and Windows platforms,
> the only problem I am having is below:
>
> use Win32::Process;
>
> I have to use the above only for windows since later in my script:
> if ($platform eq 'UNIX') {
> system("$Cmd > /dev/null 2>&1 &");
> }
> elsif ($platform eq 'WINDOWS') {
> Win32::Process::Create($process,"$^X","$^X" . " $Cmd",0,
> CREATE_NO_WINDOW, ".") || die $!;
> }
>
>
> I don't want to maintain two scripts, and it seems I cann't do something
> like this on the top of the script:
> if ($platform eq 'WINDOWS') {
> use Win32::Process;
> }
>
>
> Anyone can help?
>
> NOTE: regarding the above platform variable, it is defined on the top of
> the script:
> ($^O =~ /Win32/) ? ($platform = 'WINDOWS') : ($platform = 'UNIX');

Hello Mahdi (have I picked the right first name?)

A call to 'use' can't be put within a conditional, but one to 'require' can.
Simply replace

use Win32::Process;

with

BEGIN {
if ($^O eq 'MSWin32') {
require Win32::Process;
Win32::Process->import;
}
}

and you should be fine.

HTH,

Rob
.



Relevant Pages

  • Re: Perl scrpit to reschedule AT command?
    ... I once saw a perl script that was able to reschedule the AT command ... suggest I use the unix at command to schedule a job. ...
    (comp.lang.perl.misc)
  • Re: Running the same perl script on Win32 and Unix
    ... Rob Dixon wrote: ... > I have a perl script that will run on both Unix and Windows platforms, ...
    (perl.beginners)
  • [NEWS] IBM Informix Web DataBlade Local Root by Design
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... that ease development of "intelligent", interactive, Web-enabled database ... person who has access to change the Perl script. ...
    (Securiteam)
  • this is not a specific linux question but more of just UNIX - tar, dump, restore command questions?
    ... I am new at a company that has a Unix server and they have no documentation ... They have a script I assume that runs a backup tape every night of just one ... assuming their either are using a tar or dump command / script. ... a restore from a tar file assuming they are using tar. ...
    (comp.os.linux.misc)
  • Re: ksh script ! How can i do this
    ... > but korn shell and unix systems. ... > I am new to scripting and have a script that I would like to write to ... > I want to pull some information out of a txt file created from dos / I ... We have noticed that your lotus notes mailbox is exceeding the 100mb ...
    (comp.unix.shell)