Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Calling a binary from a different platform

by rgren925 (Beadle)
on Oct 27, 2014 at 19:30 UTC ( #1105195=perlquestion: print w/replies, xml ) Need Help??

rgren925 has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I'm calling a program with backticks from my perl script so I can get both the output text and exit code. Most of the called programs are bash or perl scripts. Occasionally a called program will be a compiled binary. Obviously, if the program was compiled on a different platform, it'll fail and throw an error to STDERR. Example, running perl script on Solaris trying to execute a binary compiled on linux.

Is there a way to test, up front, if the program I'm calling was compiled for the this platform? I'd like to just skip the program call if I can tell it won't work

The call returns a -1, but still throws the error. I tried redirecting STDERR to /dev/null, but then I no longer receive the -1.


$text = `$command 2> /dev/null`; $retCode = $?;

Thanks very much, Rick

Replies are listed 'Best First'.
Re: Calling a binary from a different platform
by roboticus (Chancellor) on Oct 28, 2014 at 00:13 UTC


    Usually on a *nix box, you can use the file command to get a bit of information about the file. Often it can tell you enough information to determine whether it's totally foreign to the environment or not.

    However, it concerns me that you may be papering over a bigger problem--how are executables for different platforms winding up in your path? If it were me, I'd prefer to tackle that problem, as it smacks of some system administration problem.


    When your only tool is a hammer, all problems look like your thumb.

Re: Calling a binary from a different platform
by Laurent_R (Canon) on Oct 27, 2014 at 20:00 UTC
    Maybe you could run your external program in an eval block.
      Thanks. Just tried that. Same result. STDERR output and rc = -1 or if I redirect stderr to /dev/null, it returns a 256. I guess what I'm really asking, is there away to determine if a binary is executable on the current platform (not like a -x permissions executable, but a functional "will it execute?") without actually executing it. i.e., a pre-test.
Re: Calling a binary from a different platform
by Mr. Muskrat (Canon) on Oct 28, 2014 at 17:21 UTC

    This is not really a perl question and more of an OS question but I'll give it go. On Linux, you can use the file command to identify file contents. When used on a binary executable, it will tell you the architecture of an executable.

    $ file /bin/arch
    /bin/arch: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), stripped
    $ file /bin/unicode_start
    /bin/unicode_start: Bourne shell script text executable
    $ file /bin/view
    /bin/view: symbolic link to `vi'

    I don't have access to a Solaris box but I know that the file command does exist there so that might be a good starting point.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1105195]
Approved by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2023-09-23 17:14 GMT
Find Nodes?
    Voting Booth?

    No recent polls found