Perl IO to Ruby IO help needed 
Author Message
 Perl IO to Ruby IO help needed

Hi,

 I want to convert the following lines from Perl to Ruby.  And I cannot
figure out how to load the file into memory:

The Perl file:

open (DEF, "<$input_def") || die "$0: cannot read $input_def\n";
$/ = undef;             # read the entire file...
$_ = <DEF>;               # ...into memory
close(DEF);

$sep = "\003";                # Use cntl-C as a separator between elements
s/\n\s*-\s/\n$sep /g;   # replace leading dashes with separator
s/#[^\n]*\n/ /g;        # delete comments
s/\s+/ /g;              # replace all newlines with space

...

  Thanks
    Craig

--
___________________________________________________________________________
 Craig Files, PhD                           |                  /
  http://www.*-*-*.com/ ~cfiles/   |       __o       /

____________________________________________|_____()/ ()____/ (970)288-0183



Sun, 08 Aug 2004 01:35:55 GMT  
 Perl IO to Ruby IO help needed

Quote:

>  I want to convert the following lines from Perl to Ruby.  And I cannot
> figure out how to load the file into memory:

> The Perl file:

> open (DEF, "<$input_def") || die "$0: cannot read $input_def\n";
> $/ = undef;                # read the entire file...
> $_ = <DEF>;          # ...into memory
> close(DEF);

[...]

What about sonething like

content = nil
input_def = '/etc/passwd'

File.open(input_def, 'r') { |f|
    content = f.read

Quote:
}

puts "got >>>#{content}<<<";

you can catch the exceptions if you want to handle the open errors yourself.

Hope this helps,

Mike

--

http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA

http://www.starnix.com/            |                  75D2 9EC4 C1C0 0599 13DA



Sun, 08 Aug 2004 01:59:35 GMT  
 Perl IO to Ruby IO help needed

Quote:

> Hi,

>  I want to convert the following lines from Perl to Ruby.
> open (DEF, "<$input_def") || die "$0: cannot read $input_def\n";
> $/ = undef;                # read the entire file...
> $_ = <DEF>;          # ...into memory
> close(DEF);

> $sep = "\003";           # Use cntl-C as a separator between elements
> s/\n\s*-\s/\n$sep /g;      # replace leading dashes with separator
> s/#[^\n]*\n/ /g;   # delete comments
> s/\s+/ /g;         # replace all newlines with space

Something like

begin
   open(ARGV[0],'r') do |file|

   end
rescue
   raise "cannot read #{ARGV[0]}"
end

   (line.gsub /\A\s*-\s/,"\003").
         gsub /\n/, " "
end


   not(line =~ /\A\s*#.*\z/)
end

Tobi

--

* peace&love.
* http://www.pinkjuice.com/



Sun, 08 Aug 2004 02:51:44 GMT  
 Perl IO to Ruby IO help needed

Thanks to you and Mike Stok,

Boy was I over thinking this problem!

Quote:

>> Hi,

>>  I want to convert the following lines from Perl to Ruby.

>> open (DEF, "<$input_def") || die "$0: cannot read $input_def\n"; $/ =
>> undef;         # read the entire file... $_ = <DEF>;             # ...into memory
>> close(DEF);

>> $sep = "\003";               # Use cntl-C as a separator between elements
>> s/\n\s*-\s/\n$sep /g;  # replace leading dashes with separator
>> s/#[^\n]*\n/ /g;       # delete comments
>> s/\s+/ /g;             # replace all newlines with space

> Something like

> begin
>    open(ARGV[0],'r') do |file|

>    end
> rescue
>    raise "cannot read #{ARGV[0]}"
> end


>    (line.gsub /\A\s*-\s/,"\003").
>          gsub /\n/, " "
> end


>    not(line =~ /\A\s*#.*\z/)
> end

> Tobi

--
___________________________________________________________________________
 Craig Files, PhD                           |                  /
  http://coxcomb.ftc.agilent.com/~cfiles/   |       __o       /

____________________________________________|_____()/ ()____/ (970)288-0183


Sun, 08 Aug 2004 04:33:14 GMT  
 Perl IO to Ruby IO help needed
<posted & mailed>

Quote:

> open (DEF, "<$input_def") || die "$0: cannot read $input_def\n";
> $/ = undef;           # read the entire file...
> $_ = <DEF>;           # ...into memory
> close(DEF);

Tobias and Mike both gave you nice examples with exception handling; in the
most simple, direct from perl translation:

  lines = IO.readlines(input_def)

This does everything you do in your perl code; it dies (with an exception)
if there is an error, and (AFAIK) the stream is automatically closed after
reading.  It's also only one line long, and much easier to understand :-)

-- SER



Tue, 10 Aug 2004 01:13:14 GMT  
 Perl IO to Ruby IO help needed

Quote:

><posted & mailed>


>> open (DEF, "<$input_def") || die "$0: cannot read $input_def\n";
>> $/ = undef;           # read the entire file...
>> $_ = <DEF>;           # ...into memory
>> close(DEF);

> Tobias and Mike both gave you nice examples with exception handling; in the
> most simple, direct from perl translation:

>   lines = IO.readlines(input_def)

> This does everything you do in your perl code; it dies (with an exception)
> if there is an error, and (AFAIK) the stream is automatically closed after
> reading.  It's also only one line long, and much easier to understand :-)

But it returns an array of \n terminated strings in lines rather than the
whole content of whatever's named in input_def as a single string. (It refers
to the IO.readlines example above).

Whether that is an important difference to the original poster I don't know,
but to steal your clarity...

  content = File.open(input_def).read

might be closer?

Mike

P.S. in perl it might be kinder & gentler to localise the modification to $/
if the program were bigger tahn a few lines...

--

http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA

http://www.starnix.com/            |                  75D2 9EC4 C1C0 0599 13DA



Tue, 10 Aug 2004 02:53:59 GMT  
 Perl IO to Ruby IO help needed

Quote:

>   content = File.open(input_def).read

To make sure the file gets closed:

  content = File.open(input_def) {|f| f.read}



Tue, 10 Aug 2004 03:18:12 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. mixing IO#read and IO#syswrite

2. IMPORT IO, FROM IO IMPORT ?

3. IMPORT IO, FROM IO IMPORT ?

4. IMPORT IO, FROM IO IMPORT ?

5. BUG?: HP-UX 10.20 and IO#gets followed by IO#puts

6. BUG? IO.seek conflicts with IO.sysread

7. VAX Ada sequential IO and Direct IO - (nf)

8. VAX Ada sequential IO and Direct IO

9. VAX Ada sequential IO and Direct IO

10. Not DOS interrupts, I want direct IO (memory mapped IO)

11. I need help ... (IO-Probs)

12. Need IO Optimization help

 

 
Powered by phpBB® Forum Software