Calling a Perl script from Ruby

1

12 34

I'd like to use gpsPhoto.pl in a Ruby script.
<http://www.carto.net/projects/photoTools/gpsPhoto/>

Assuming I have the script on my computer.

Syntax from command line is:

perl gpsPhoto.pl --dir photos/ --gpsfile GPSTrackWithTime.gpx
--timeoffset 10800

Can I include this in a Ruby script? Please don't suggest porting it to
Ruby, it's 400 lines,

Thanks
 
T

Tim Hunter

12 said:
perl gpsPhoto.pl --dir photos/ --gpsfile GPSTrackWithTime.gpx
--timeoffset 10800

Can I include this in a Ruby script? Please don't suggest porting it to
Ruby, it's 400 lines,
Check out the system() method.
 
D

Dave Coleman

Tim said:
Check out the system() method.
looks like system only returns true or false if the system call executes
successfully. What if you want to evaluate what's returned from that
perl script?
 
R

Ron Valente

Dave said:
looks like system only returns true or false if the system call executes
successfully. What if you want to evaluate what's returned from that
perl script?
backticks my friend :)
 
M

M. Edward (Ed) Borasky

Dave said:
looks like system only returns true or false if the system call executes
successfully. What if you want to evaluate what's returned from that
perl script?
IIRC "system" returns the (implementation-dependent) numerical return
code of the call, where by tradition "0" is success and non-zero is an
application-dependent error number. If Ruby is returning "true" on
success and "false" on failure, that's different from what many
environments do.
 
J

James Edward Gray II

IIRC "system" returns the (implementation-dependent) numerical return
code of the call, where by tradition "0" is success and non-zero is an
application-dependent error number. If Ruby is returning "true" on
success and "false" on failure, that's different from what many
environments do.

It does, but you can get the exit code:

system("date") # => true
$?.to_i # => 0
# >> Sat Jul 14 23:11:49 CDT 2007

James Edward Gray II
 
C

Corey Jewett

It does, but you can get the exit code:

system("date") # => true
$?.to_i # => 0
# >> Sat Jul 14 23:11:49 CDT 2007

There is also Kernel.`` which will return stdout. If you're looking
for something more powerful still look at popen, Open3, or (best of
all ;D) the Open4 gem.

Corey
 
1

12 34

Original poster here.

Thanks to all. You've given me things to look at. And fortunately I
don't have to interact with the script, nor do I need a response. There
is some information I'd like back, but I'm not sure without reading up
on it whether the script supplies the information.

Thanks again.
 
1

12 34

Ron said:
backticks my friend :)

Thanks, backticks are my friend. The time delay in responding was due to
sorting out the Perl script--my issues a a minor bug in the script which
was quickly fixed.

I want to be able to use Ruby variables in the Perl script. Here's the
command line script:
perl gpsPhoto.pl --dir photos/ --gpsfile GPSTrackWithTime.gpx
--timeoffset )

I can hard code the location of the gpsPhoto.pl, dir, etc, but I want to
use Ruby variables, e.g.

`perl gpsPhotoLoc --dir destPhoto --gpsfile gpxFiles --timeoffset 0`

where e.g.

"/Users/me/Library/Scripts/Digital Camera Scripts/gpsPhoto.w.pl"

Thanks for any suggestions
 
1

12 34

Answering own question:

gpxFiles = gpxDateFN.join("\" \" ")
`perl \"#{gpsPhotoLoc}\" --dir \"#{destPhoto}\" --gpsfile
\"#{gpxFiles}\" --timeoffset #{timeOffset}`

All the double quotes are needed because the file names have spaces in
them, and gpxFiles is a list of file names with a space in between each
item. Rather ugly solution. Open to suggestions on getting a string list
from an array with double quotes around each item and a space between
each item.

But it's working I think. I need to exercise it some to make sure.
 
S

Suraj Kurapati

12 said:
Answering own question:

gpxFiles = gpxDateFN.join("\" \" ")
`perl \"#{gpsPhotoLoc}\" --dir \"#{destPhoto}\" --gpsfile
\"#{gpxFiles}\" --timeoffset #{timeOffset}`

All the double quotes are needed because the file names have spaces in
them, and gpxFiles is a list of file names with a space in between each
item. Rather ugly solution.

class String
# Transforms this string into an escaped POSIX shell argument.
def shell_escape
inspect.gsub(/\\(\d{3})/) {$1.to_i(8).chr}
end
end
Open to suggestions on getting a string list
from an array with double quotes around each item and a space between
each item.

your_array.map {|s| s.shell_escape}.join(' ')
 
1

12 34

Suraj said:
12 34 wrote:

class String
# Transforms this string into an escaped POSIX shell argument.
def shell_escape
inspect.gsub(/\\(\d{3})/) {$1.to_i(8).chr}
end
end


your_array.map {|s| s.shell_escape}.join(' ')

Thank you. Much more Rubyesh.
 
S

Suraj Kurapati

Ryan said:

Wow, is this your Perl handler for RubyInline (which was mentioned by
Eric Hodel on this thread:
http://www.ruby-forum.com/topic/83025#147160)?

I've been waiting for months for any information on how to use Perl
modules from within Ruby (I'm trying to convince my employer to use Ruby
but they won't consider it unless Ruby can leverage their existing Perl
modules for the time being).

Could you please tell us more about your Perl handler? I don't see it
listed in the Zen Spider Software website.

Thanks for your consideration.
 
M

M. Edward (Ed) Borasky

Suraj said:
I've been waiting for months for any information on how to use Perl
modules from within Ruby (I'm trying to convince my employer to use Ruby
but they won't consider it unless Ruby can leverage their existing Perl
modules for the time being).
You can always use RSpec to leverage Ruby into almost any situation. :)
 
S

Suraj Kurapati

M. Edward (Ed) Borasky said:
You can always use RSpec to leverage Ruby into almost any situation. :)

RSpec is indeed a tasty treat. However, change is not driven by
external temptation alone; it also requires internal effort. In other
words, it is as the Buddha once said: "first intention, then
enlightenment".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,089
Members
48,773
Latest member
Kaybee

Latest Threads

Top