translate Perl diamond operator to Ruby

C

Chad Perrin

Over the years, I've found the following to be an excellent way to whip
up quick, very useful Perl scripts:

#!/usr/bin/perl

while(<>) {
# do stuff . . .
}

Is there an equivalent idiom in Ruby? So far as I'm aware, there's no
catch-all diamond operator in Ruby that allows one to create a default
input behavior for a script that accepts either a filename or piped
output of another command the way this works in Perl. I certainly hope
there's an equivalent, though.
 
K

Ken Bloom

Over the years, I've found the following to be an excellent way to whip
up quick, very useful Perl scripts:

#!/usr/bin/perl

while(<>) {
# do stuff . . .
}

Is there an equivalent idiom in Ruby? So far as I'm aware, there's no
catch-all diamond operator in Ruby that allows one to create a default
input behavior for a script that accepts either a filename or piped
output of another command the way this works in Perl. I certainly hope
there's an equivalent, though.

ARGF.each do |line|
# do stuff
end
 
B

Brian Candler

Over the years, I've found the following to be an excellent way to whip
up quick, very useful Perl scripts:

#!/usr/bin/perl

while(<>) {
# do stuff . . .
}

Is there an equivalent idiom in Ruby? So far as I'm aware, there's no
catch-all diamond operator in Ruby that allows one to create a default
input behavior for a script that accepts either a filename or piped
output of another command the way this works in Perl. I certainly hope
there's an equivalent, though.

while gets
# do stuff with $_
end

Or, less perly,

while line = gets
# do stuff with line
end

Or, if there are multiple files listed on ARGV, and you want to slurp them
all one at a time,

while contents = gets(nil)
# do stuff with contents
end

Doing that in Perl seems quite hard.

B.
 
C

Chad Perrin

What _is_ ARGF? I've printed it's class, but apparently it is:


... an Object.

Now that I know ARGF exists . . .

http://www.danvk.org/wp/?m=200701 reports the following:

ARGF is a great crutch for Perl programmers who miss typing while(<>)
{...}. It opens each input file left in ARGV and yields each line. If
there´s no input files left, it reads STDIN and yields each line it
gets there. Many, many programs do their work in an ARGF loop.
 
C

Chad Perrin

ARGF.each do |line|
# do stuff
end

Thanks muchly. Armed with this seed of knowledge (to mix a metaphor), I
can find the rest via Google easily. I appreciate it.
 
C

Chad Perrin

while contents = gets(nil)
# do stuff with contents
end

Doing that in Perl seems quite hard.

Thanks for the information. As for this last example, the functionality
you achieved isn't exactly "hard" in Perl, but it's a touch less
intuitive. Slurping a file via the diamond operator should end up
looking something like this, generally:

my $foo;
{ local $/; $foo = <>; }

The "my $foo" part, for those not familiar with Perl, is just the way
the $foo variable can be declared with lexical scope. If you're writing
code without strict and warnings pragmas (indispensable debugging aids
in Perl), you could dispense with the "my $foo" line altogether.

In fact, you could dispense with the braces and use "undef $/" instead
of "local $/" if you prefer, as long as you don't care about getting the
original value of $/ back (or want to put it back in manually for some
reason).

TIMTOWTDI.
 
J

James Edward Gray II

Or, less perly,

while line = gets
# do stuff with line
end

I really feel the Ruby version is to use a standard iterator:

ARGF.each do |line|
# do stuff with line
end
Or, if there are multiple files listed on ARGV, and you want to
slurp them
all one at a time,

while contents = gets(nil)
# do stuff with contents
end

contents = ARGF.read

James Edward Gray II
 
J

James Edward Gray II

Now that I know ARGF exists . . .

http://www.danvk.org/wp/?m=3D200701 reports the following:

ARGF is a great crutch for Perl programmers who miss typing while=20
(<>)
{...}. It opens each input file left in ARGV and yields each =20
line. If
there=B4s no input files left, it reads STDIN and yields each line = it
gets there. Many, many programs do their work in an ARGF loop.

I don't much care for that description. ARGF simplifies the =20
implementation of some very common behavior for command-line =20
programs. I don't feel it exists merely as a crutch for Perl =20
programmers.

James Edward Gray II=
 
C

Chad Perrin

I don't much care for that description. ARGF simplifies the
implementation of some very common behavior for command-line
programs. I don't feel it exists merely as a crutch for Perl
programmers.

Nor do I. I found the more implementation-related description to be
somewhat useful, however, once I got past the opining.
 
R

Robert Klemme

Thanks muchly. Armed with this seed of knowledge (to mix a metaphor), I
can find the rest via Google easily. I appreciate it.

You can also use one of the command line switches -n or -p, e.g.

13:04:42 [tmp]: ls -l | ruby -ne 'puts "<#{$_[0,3]}>"'
<tot>
<-rw>
<lrw>
13:04:59 [tmp]:

Kind regards

robert
 
C

Chad Perrin

Thanks muchly. Armed with this seed of knowledge (to mix a metaphor), I
can find the rest via Google easily. I appreciate it.

You can also use one of the command line switches -n or -p, e.g.

13:04:42 [tmp]: ls -l | ruby -ne 'puts "<#{$_[0,3]}>"'
<tot>
<-rw>
<lrw>
13:04:59 [tmp]:

Kind regards

robert

That's useful, and very similar to Perl, but in practice I find that I
don't take that approach very often at all. The only command/shebang
line switch I use with any frequency in Perl is -l, for adding a newline
to the end of all print statements, and since Ruby has puts I don't need
to do that in Ruby. Usually, the programs I find myself writing don't
neatly fit into a while loop -- except when writing a toy script to
demonstrate something, in which case it usually pays to be explicit with
the while loop anyway -- so the while (<>) { . . . } idiom in Perl (and
now the ARGF.each do |foo| . . . end idiom in Ruby) is far more useful
to me.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top