# Catching an error running a Perl script from a Ruby script

**URL:** <https://www.ruby-forum.com/t/catching-an-error-running-a-perl-script-from-a-ruby-script/208487>\
**Category:** Ruby\
**Created:** [June 20, 2011, 3:44am UTC](https://www.ruby-forum.com/t/catching-an-error-running-a-perl-script-from-a-ruby-script/208487 "2011-06-20T03:44:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [June 20, 2011, 3:44am UTC](https://www.ruby-forum.com/t/catching-an-error-running-a-perl-script-from-a-ruby-script/208487/1 "2011-06-20T03:44:51Z")

</div>

I’m running a Perl script from within a Ruby script. If the script  
throws an error the Ruby script continues. I’d like to catch the error  
and do something. I’m running the script in TextMate and the errors do  
show up in the TextMate “Running” window.

Here’s the script and how I tried to catch it. The script is a published  
script: gpsPhoto.pl

begin # This isn’t catching errors  
`perl \"#{gpsPhotoLoc}\" --image \"#{imageFile}\" --gpsfile \"#{gpxFile}\" --timeoffset #{geoOffset} --maxtimediff #{maxTimeDiff} --maxdistance #{maxDistance} --geoinfo wikipedia --city auto --state guess --country guess`  
rescue  
puts “Error calling gpsPhoto.pl:” + $! + " for photo:  
#{imageFile}"  
end

The Perl script reads the GPS information and goes [geonames.org](http://geonames.org) and get  
location information, e.g. city and state. I think the errors are caused  
by problems at the site because the errors aren’t repeatable (I do  
hundreds of photos in a batch and the errors vary from run to run for  
the same photos.

Thanks for any suggestions.

---

<div class="post-metadata">

**Author:** ![mtnbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mtnbiker](https://www.ruby-forum.com/u/mtnbiker)\
**Post date:** [June 20, 2011, 9:16am UTC](https://www.ruby-forum.com/t/catching-an-error-running-a-perl-script-from-a-ruby-script/208487/2 "2011-06-20T09:16:43Z")

</div>

Greg \_\_ wrote in post #1006261:

> I’m running a Perl script from within a Ruby script. If the script  
> throws an error the Ruby script continues. I’d like to catch the error  
> and do something.

You’re not going to be able to do that in the sense that you catch an  
exception. Essentially, you are using your ruby program to tell your  
operating system to go off and run another program for you.

What you can do is examine the exit code that the operating system got  
from running your program using ruby’s system() method (0=success,  
anything else = failure).

Or, you can use ruby’s backticks to run the program, which will return  
any _string_ the program writes to stdout. The error _string_  
from your perl script, however, is being sent to stderr. You have a  
couple  
of options in that regard:

1. You can use shell redirection commands to send the stderr _string_  
into the nearest black hole

cmd = “perl my\_prog.pl 2\>/dev/null”

2 represents stderr, and ‘\>’ redirects stderr’s output to ‘/dev/null’,  
which is a place on your operating system where unwanted text can be  
sent to never  
be seen again.

1. Or, you can redirect the stderr _text_ to stdout:

cmd = “perl my\_prog.pl 2\>&1”

1 represents stdout. If you do that, ruby’s backticks will catch any  
strings the program sends to stdout  
and stderr. ruby’s backticks also set the variable $?, which is the  
process exit code (0=success, anything else = failure). So you could  
‘catch’ the error message _text_ and check if the process exited  
successfully like this:

cmd = “perl perl.pl 2\>&1”  
stdout\_output = `#{cmd}`

puts “\*\* #{stdout\_output} \*\*”

if $? != 0  
puts “-- perl script error! —”  
end

> puts “Error calling gpsPhoto.pl:” + $! + " for photo:

ruby copies a lot of things from perl, and the $! in that line is the  
current exception info for your ruby program–not your perl program.  
Your ruby program doesn’t know anything about perl, and it has no idea  
whether your perl program threw an exception or not. All your ruby  
program can do is capture some _text_ that your perl program outputs.

> I’m running the script in TextMate and the errors do  
> show up in the TextMate “Running” window.

Don’t ever run programs that use subprocesses in an IDE–you can never  
be sure whether the IDE is handling the subprocess correctly. Execute  
your program from the command line.
