Multi-Lingual Ruby

I was following a Java VS Perl discussion on a web board that I read.
Naturally I wanted to see what the Ruby version would look like, so I
grabbed the Perl code and and pasted into a file. I commented out all
the Perl code and inserted the ruby version. The file looked like this

#!/usr/bin/perl -w

use strict;

my %frequency = ();

$frequency{$_}++ for (split /\W/, <>);

print “$: $frequency{$}\n” for (keys %frequency);

frequency = Hash.new(0)
ARGF.read.split(/\W/).each { |w| next if w == ‘’; frequency[w] += 1}
frequency.each { |k,v| puts “#{k}: #{v}” }

I then ran the program like this …

ruby wordfreq.rb inputfile

And got the response …

Unquoted string “frequency” may clash with future reserved word at
wordfreq.rb line 7.
Semicolon seems to be missing at wordfreq.rb line 7.
Unquoted string “w” may clash with future reserved word at wordfreq.rb
line 8.
Unquoted string “w” may clash with future reserved word at wordfreq.rb
line 8.
Unquoted string “frequency” may clash with future reserved word at
wordfreq.rb line 8.
Unquoted string “w” may clash with future reserved word at wordfreq.rb
line 8.
syntax error at wordfreq.rb line 8, near ")
ARGF"
Execution of wordfreq.rb aborted due to compilation errors.

What!? These didn’t look like Ruby error messages! Is Ruby now
embedding a perl interpreter?

Actually, I understand what happened. Ruby looked at the #! comment and
politely loaded the Perl interpreter. It just surprized me for a
moment.

···


– Jim Weirich jweirich@one.net http://w3.one.net/~jweirich

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)

HINT: Look at the top line of the file…

:wink:

Chris

···

----- Original Message -----
From: “Jim Weirich” jweirich@one.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, April 19, 2003 7:41 PM
Subject: Multi-Lingual Ruby

I was following a Java VS Perl discussion on a web board that I read.
Naturally I wanted to see what the Ruby version would look like, so I
grabbed the Perl code and and pasted into a file. I commented out all
the Perl code and inserted the ruby version. The file looked like this

#!/usr/bin/perl -w

use strict;

my %frequency = ();

$frequency{$_}++ for (split /\W/, <>);

print “$: $frequency{$}\n” for (keys %frequency);

frequency = Hash.new(0)
ARGF.read.split(/\W/).each { |w| next if w == ‘’; frequency[w] += 1}
frequency.each { |k,v| puts “#{k}: #{v}” }

I then ran the program like this …

ruby wordfreq.rb inputfile

And got the response …

Unquoted string “frequency” may clash with future reserved word at
wordfreq.rb line 7.
Semicolon seems to be missing at wordfreq.rb line 7.
Unquoted string “w” may clash with future reserved word at wordfreq.rb
line 8.
Unquoted string “w” may clash with future reserved word at wordfreq.rb
line 8.
Unquoted string “frequency” may clash with future reserved word at
wordfreq.rb line 8.
Unquoted string “w” may clash with future reserved word at wordfreq.rb
line 8.
syntax error at wordfreq.rb line 8, near ")
ARGF"
Execution of wordfreq.rb aborted due to compilation errors.

What!? These didn’t look like Ruby error messages! Is Ruby now
embedding a perl interpreter?

Actually, I understand what happened. Ruby looked at the #! comment and
politely loaded the Perl interpreter. It just surprized me for a
moment.


– Jim Weirich jweirich@one.net http://w3.one.net/~jweirich

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)

DOH!

I am so embarrassed… I sent that off before I finished reading your
post…

Actually, you shouldn’t be, since that seems to (somehow) be the
source of the problem. Make it ##!/… and the problem goes away.

The question is why this influences ruby…

···

On Sun, 20 Apr 2003 13:43:59 +0900 “Chris Pine” nemo@hellotree.com wrote:

DOH!

I am so embarrassed… I sent that off before I finished reading your
post…


Ryan Pavlik rpav@users.sf.net

“We’ve found a dilly of a pickle of a mystery!” - 8BT

No problem … I’ve been burned enough that I read an entire thread
before dashing off a quick reply. :slight_smile:

···

On Sun, 2003-04-20 at 00:43, Chris Pine wrote:

DOH!

I am so embarrassed… I sent that off before I finished reading your
post…


– Jim Weirich jweirich@one.net http://w3.one.net/~jweirich

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)

Actually, you shouldn’t be, since that seems to (somehow) be the
source of the problem. Make it ##!/… and the problem goes away.

The question is why this influences ruby…

Indeed, this is an odd behaviour. Below is another example. It certainly
looks like ruby is calling Perl, but I don’t see why it should.

dcarrera ~ $ cat > example
#!/usr/bin/perl -w

puts “Hello World”
dcarrera ~ $
dcarrera ~ $ ruby example
Unquoted string “puts” may clash with future reserved word at example line
3.
String found where operator expected at example line 3, near “puts “Hello
World””
(Do you need to predeclare puts?)
syntax error at example line 3, near “puts “Hello World””
Execution of example aborted due to compilation errors.
dcarrera ~ $

···


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

Ryan Pavlik rpav@nwlink.com writes:

The question is why this influences ruby…

In the Perl documentation, it says

   If the #! line does not contain the word "perl", the pro-
   gram named after the #! is executed instead of the Perl
   interpreter.  This is slightly bizarre, but it helps peo-
   ple on machines that don't do #!, because they can tell a
   program that their SHELL is /usr/bin/perl, and Perl will
   then dispatch the program to the correct interpreter for
   them.

Perhaps Matz reasoned similarly for Ruby.

Tim

Perl allows the same thing. Consider the file masquerade.pl …

$ cat masquerade.pl
#!/usr/bin/env ruby
puts “HA! This is really a Ruby Program!”
$
$ perl masquerade.pl
HA! This is really a Ruby Program!

This is a stealth measure that allows us Rubists to write code in a
perl-only shop. :slight_smile:

···

On Sun, 2003-04-20 at 01:21, Daniel Carrera wrote:

Actually, you shouldn’t be, since that seems to (somehow) be the
source of the problem. Make it ##!/… and the problem goes away.

The question is why this influences ruby…


– Jim Weirich jweirich@one.net http://w3.one.net/~jweirich

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)

Scripsit ille »Tim Heaney« heaney@cablespeed.com:

Ryan Pavlik rpav@nwlink.com writes:

The question is why this influences ruby…

In the Perl documentation, it says

   If the #! line does not contain the word "perl", the pro-
   gram named after the #! is executed instead of the Perl
   interpreter.  This is slightly bizarre, but it helps peo-
   ple on machines that don't do #!, because they can tell a
   program that their SHELL is /usr/bin/perl, and Perl will
   then dispatch the program to the correct interpreter for
   them.

Perhaps Matz reasoned similarly for Ruby.

But it’s implemented differently.

rpolzer@katsuragi tmp $ ls -l r
lrwxrwxrwx 1 rpolzer users 13 2003-04-20 21:35 r → /usr/bin/ruby

rpolzer@katsuragi tmp $ cat test
#!/tmp/r
print “Hello\n”

rpolzer@katsuragi tmp $ ruby test
test:1: Can’t exec /tmp/r (fatal)

That took very LONG - and the reason:

rpolzer@katsuragi tmp $ strace ruby test 2>&1 |grep ^exec
execve(“/usr/bin/ruby”, [“ruby”, “test”], [/* 62 vars /]) = 0
execve(“/tmp/r”, [“/tmp/r”, “test”], [/
62 vars /]) = 0
execve(“/tmp/r”, [“/tmp/r”, “test”], [/
62 vars /]) = 0
execve(“/tmp/r”, [“/tmp/r”, “test”], [/
62 vars /]) = 0
execve(“/tmp/r”, [“/tmp/r”, “test”], [/
62 vars /]) = 0
execve(“/tmp/r”, [“/tmp/r”, “test”], [/
62 vars /]) = 0
execve(“/tmp/r”, [“/tmp/r”, “test”], [/
62 vars /]) = 0
execve(“/tmp/r”, [“/tmp/r”, “test”], [/
62 vars /]) = 0
execve(“/tmp/r”, [“/tmp/r”, “test”], [/
62 vars /]) = 0
execve(“/tmp/r”, [“/tmp/r”, “test”], [/
62 vars /]) = 0
[…]
execve(“/tmp/r”, [“/tmp/r”, “test”], [/
62 vars /]) = 0
execve(“/tmp/r”, [“/tmp/r”, “test”], [/
62 vars */]) = -1 EMFILE (Too many open files)

Perl, contrary, doesn’t do that with a similar setup:

execve(“/usr/bin/perl”, [“perl”, “test”], [/* 62 vars /]) = 0
execve(“/tmp/p”, [“/tmp/p”, “test”], [/
62 vars */]) = 0

and then happily runs the script. Probably it checks if the script
interpreter names match (argv[0] and the #! line). I think it can be
shown like that:

rpolzer@katsuragi tmp $ perl test
Can’t exec /tmp/pa at test line 1.

but

rpolzer@katsuragi tmp $ ./p test
Hello

while ./p is symlinked to /usr/bin/perl. Why? #!/tmp/pa contains the
substring ‘p’…

···

Hatte ~1a lang $NONAMELAPTOP als Arbeitsplatz, der wurde
auch recht warm. Einmal so warm, daß […] das Display einfror […]
Das verletzt den zweiten Hauptsatz der Thermodynamik.
[Sebastian Posner und Stefan Froehlich in dasr]

It also works for the shells (then again, I would have expected that).
But it doesn’t work for Python:

dcarrera ~ $ cat example.pl
#!/usr/bin/env ruby
puts “Hello World”
dcarrera ~ $ perl example.pl
Hello World
dcarrera ~ $ sh example.pl
Hello World
dcarrera ~ $ tcsh example.pl
Hello World
dcarrera ~ $ csh example.pl
Hello World
dcarrera ~ $ python example.pl
File “example.pl”, line 2
puts “Hello World”
^
SyntaxError: invalid syntax
dcarrera ~ $

···

On Sun, Apr 20, 2003 at 02:40:09PM +0900, Jim Weirich wrote:

Perl allows the same thing. Consider the file masquerade.pl …


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

This is a stealth measure that allows us Rubists to write code in a
perl-only shop. :slight_smile:

So that, some time later, you can declare,
“Ruby? You’re soaking in it!”

(With apologies to those unfamiliar with 20th century USA TV commercials.)

James

Can we infer something on the evilness of languages (as in ‘intention
to take on the world’) :wink: ?

···

On Sun, Apr 20, 2003 at 02:49:40PM +0900, Daniel Carrera wrote:

On Sun, Apr 20, 2003 at 02:40:09PM +0900, Jim Weirich wrote:

Perl allows the same thing. Consider the file masquerade.pl …

It also works for the shells (then again, I would have expected that).
But it doesn’t work for Python:

dcarrera ~ $ cat example.pl
#!/usr/bin/env ruby
puts “Hello World”
dcarrera ~ $ perl example.pl
Hello World
dcarrera ~ $ sh example.pl
Hello World
dcarrera ~ $ tcsh example.pl
Hello World
dcarrera ~ $ csh example.pl
Hello World
dcarrera ~ $ python example.pl
File “example.pl”, line 2
puts “Hello World”
^
SyntaxError: invalid syntax
dcarrera ~ $


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Save yourself from the ‘Gates’ of hell, use Linux." – like that one.
– The_Kind @ LinuxNet