Calling a Perl script from Ruby

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

···

--
Posted via http://www.ruby-forum.com/.

12 34 wrote:

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.

···

--
RMagick OS X Installer [http://rubyforge.org/projects/rmagick/\]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618\]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html\]

Is there anything wrong with:

output_string = `perl gpsPhoto.pl [blah, blah, ....]`

?

The backticks ` are the part to note carefully.

"12 34" <rubyforum@web.knobby.ws> wrote in message news:eb1ea51ab274d8c0fbc362c5770f2d5a@ruby-forum.com...

···

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

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

--
Posted via http://www.ruby-forum.com/\.

Tim Hunter wrote:

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?

···

--
Posted via http://www.ruby-forum.com/\.

Dave Coleman wrote:

Tim Hunter wrote:
  

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?

backticks my friend :slight_smile:

Dave Coleman wrote:

Tim Hunter wrote:

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?

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

···

On Jul 14, 2007, at 9:44 PM, M. Edward (Ed) Borasky wrote:

Dave Coleman wrote:

Tim Hunter wrote:

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?

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.

Ron Mr. wrote:

Dave Coleman wrote:

Tim Hunter wrote:
  

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?

backticks my friend :slight_smile:

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

···

--
Posted via http://www.ruby-forum.com/\.

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

···

On Jul 14, 2007, at 9:12 PM, James Edward Gray II wrote:

On Jul 14, 2007, at 9:44 PM, M. Edward (Ed) Borasky wrote:

Dave Coleman wrote:

Tim Hunter wrote:

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?

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

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.

···

--
Posted via http://www.ruby-forum.com/.

or you could get really gross:

http://vanity.zenspider.com.nyud.net:8090/~ryan/mov/perl2.mov

(no, I don't actually recommend this approach--it is just funny)

···

On Jul 22, 2007, at 23:16 , 12 34 wrote:

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 )

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.

···

--
Posted via http://www.ruby-forum.com/.

12 34 wrote:

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(' ')

···

--
Posted via http://www.ruby-forum.com/\.

Ryan Davis wrote:

http://vanity.zenspider.com.nyud.net:8090/~ryan/mov/perl2.mov

Wow, is this your Perl handler for RubyInline (which was mentioned by
Eric Hodel on this thread:
Use Perl modules from Ruby? - Ruby - Ruby-Forum)?

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.

···

--
Posted via http://www.ruby-forum.com/\.

Suraj Kurapati wrote:

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

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(' ')

Thank you. Much more Rubyesh.

···

--
Posted via http://www.ruby-forum.com/\.

Suraj Kurapati wrote:

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. :slight_smile:

M. Edward (Ed) Borasky wrote:

Suraj Kurapati wrote:

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. :slight_smile:

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".

···

--
Posted via http://www.ruby-forum.com/\.