Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl subroutine one call for windows box other Unix

Status
Not open for further replies.

ruc

Programmer
Joined
Sep 12, 2007
Messages
2
Location
US
Hello:

I'm having difficutly getting a Perl Program to NOT send an error message on code that is meant for a Unix server I need to use a 3rd party "callAPI" and the coding is different for Windows vs. Unix so I check if it's windows call one subroutine else it's unix use another subroutine on windows when I run the code it is trying to interpret the unix-sentric code, how do I get around this?

Thanks for any help, please let me know if seeing the code will help, did not want to add it as.

RUC
 
use the core module File::Spec to find out what operating system the perl program is running on. You can use $^O (thats a capitol O not a zero) if you really wanted to.

my $os = $^O;


keep your OS specifc functions in a module and "require" them depending on the value of $os.

Code:
if ($os eq 'Win32') {
   require "Windows_functions.pm";
}
else {
   require "Unix_functions.pm";
}


Or code a hash table with references to functions depending on the OS.

Code:
my %functions;
if ($os eq 'Win32') {
   %functions = (
      $function_name => \&foo;
      $function_name2 => \&bar;
   ); 
}
else {
   %functions = (
      $function_name => \&biz;
      $function_name2 => \&baz;
   );
}

then call the functions by reference:

Code:
$function_name->($input);



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you for the quick response, and forgive me for being a little slow if I wanted to use the %function option how do you call multiple lines of code? Here is the IF statement I'm currently using to call my subroutines:
if ($^O eq 'MSWin32') {
require Win32::OLE;
call_win();
exit_job();
}
else
{
call_unix();
exit_job()";
}
 
The lines of code would be in the functions or modules. If you find there is too much difference between a Unix and Windows version of your perl script you should code them entirely seperate and not try and load in modules or functions on a conditional basis.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top