Having Ruby 1.8 decide on "File.open" or 'IO.popen'?

  • Thread starter Josef 'Jupp' Schugt
  • Start date
J

Josef 'Jupp' Schugt

Hi!

To write to (mbox) file I use "File.open('/var/spool/mail/jupp')". To
write to a (MDA) stream I would use "File.popen('/usr/bin/fetchmail')".
Is there an *existing* means that I can use for both purposes so that I
can code as follows?

SomeClass.open('/var/spool/mail/jupp')
SomeClass.open('| /usr/bin/fetchmail')

Or is there good reason *not* to do that?

Josef 'Jupp' Schugt
 
N

Neil Stevens

Hi!

To write to (mbox) file I use "File.open('/var/spool/mail/jupp')". To
write to a (MDA) stream I would use "File.popen('/usr/bin/fetchmail')".
Is there an *existing* means that I can use for both purposes so that I
can code as follows?

SomeClass.open('/var/spool/mail/jupp')
SomeClass.open('| /usr/bin/fetchmail')

Or is there good reason *not* to do that?

"| " are valid characters to start a filename, so how would you
distinguish what is intended?
 
D

David G. Andersen

Hi!

To write to (mbox) file I use "File.open('/var/spool/mail/jupp')". To
write to a (MDA) stream I would use "File.popen('/usr/bin/fetchmail')".
Is there an *existing* means that I can use for both purposes so that I
can code as follows?

SomeClass.open('/var/spool/mail/jupp')
SomeClass.open('| /usr/bin/fetchmail')

Kernel.open("/var/spool/mail/jupp")
Kernel.open("| /usr/bin/fetchmail")
Or is there good reason *not* to do that?

If there's any user-supplied data in the string and you want to
be certain you're opening a file. From a security standpoint,
File.open is better because it cannot spawn a sub-shell or invoke
other programs. This only matters in certain contexts, obviously.

-Dave
 
J

Josef 'Jupp' Schugt

Neil said:
"| " are valid characters to start a filename, so how would you
distinguish what is intended?

By means of quoting:

SomeClass.open('| /usr/bin/fetchmail') # Stream
SomeClass.open('\|\ /usr/bin/fetchmail') # File

Josef 'Jupp' Schugt
 

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

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top