Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > translate Perl diamond operator to Ruby

Reply
Thread Tools

translate Perl diamond operator to Ruby

 
 
Chad Perrin
Guest
Posts: n/a
 
      04-15-2007
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.

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"The ability to quote is a serviceable
substitute for wit." - W. Somerset Maugham

 
Reply With Quote
 
 
 
 
Ken Bloom
Guest
Posts: n/a
 
      04-15-2007
On Mon, 16 Apr 2007 03:06:58 +0900, Chad Perrin wrote:

> 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

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      04-15-2007
On Mon, Apr 16, 2007 at 03:06:58AM +0900, Chad Perrin wrote:
> 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.

 
Reply With Quote
 
Benjohn Barnes
Guest
Posts: n/a
 
      04-15-2007

On 15 Apr 2007, at 19:40, Ken Bloom wrote:

> On Mon, 16 Apr 2007 03:06:58 +0900, Chad Perrin wrote:
>
>> 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


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

> irb(main):003:0> ARGF.class
> => Object


... an Object.

 
Reply With Quote
 
Chad Perrin
Guest
Posts: n/a
 
      04-15-2007
On Mon, Apr 16, 2007 at 05:27:24AM +0900, Benjohn Barnes wrote:
>
> What _is_ ARGF? I've printed it's class, but apparently it is:
>
> >irb(main):003:0> ARGF.class
> >=> Object

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

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
McCloctnick the Lucid: "The first rule of magic is simple. Don't waste
your time waving your hands and hopping when a rock or a club will do."

 
Reply With Quote
 
Chad Perrin
Guest
Posts: n/a
 
      04-15-2007
On Mon, Apr 16, 2007 at 03:40:17AM +0900, Ken Bloom wrote:
> On Mon, 16 Apr 2007 03:06:58 +0900, Chad Perrin wrote:
>
> > 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


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

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"A script is what you give the actors. A program
is what you give the audience." - Larry Wall

 
Reply With Quote
 
Chad Perrin
Guest
Posts: n/a
 
      04-15-2007
On Mon, Apr 16, 2007 at 05:25:41AM +0900, Brian Candler wrote:
>
> 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.

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
print substr("Just another Perl hacker", 0, -2);

 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      04-16-2007
On Apr 15, 2007, at 3:25 PM, Brian Candler wrote:

> 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

 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      04-16-2007
On Apr 15, 2007, at 5:07 PM, Chad Perrin wrote:

> On Mon, Apr 16, 2007 at 05:27:24AM +0900, Benjohn Barnes wrote:
>>
>> What _is_ ARGF? I've printed it's class, but apparently it is:
>>
>>> irb(main):003:0> ARGF.class
>>> =3D> Object

>>
>> ... an Object.
>>

>
> 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=

 
Reply With Quote
 
Chad Perrin
Guest
Posts: n/a
 
      04-16-2007
On Mon, Apr 16, 2007 at 12:18:12PM +0900, James Edward Gray II wrote:
> On Apr 15, 2007, at 5:07 PM, Chad Perrin wrote:
>
> >On Mon, Apr 16, 2007 at 05:27:24AM +0900, Benjohn Barnes wrote:
> >>
> >>What _is_ ARGF? I've printed it's class, but apparently it is:
> >>
> >>>irb(main):003:0> ARGF.class
> >>>=> Object
> >>
> >>... 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.

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

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"It's just incredible that a trillion-synapse computer could actually
spend Saturday afternoon watching a football game." - Marvin Minsky

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
diamond operator Roedy Green Java 25 05-06-2012 12:07 AM
Odd behavior of operator=() in diamond inheritance situation Juha Nieminen C++ 1 03-02-2009 12:00 PM
translate this send into perl sfgroups@gmail.com Perl 1 03-16-2005 09:54 PM
<> the Diamond Operator maryellen sniffen Perl Misc 3 12-31-2004 03:07 PM
binmode and the diamond operator J. Romano Perl Misc 6 11-17-2003 02:47 AM



Advertisments