handle to /dev/null?

P

peterkayatwork

Hi,

I want to be able to attempt to open a file handle on a file, and if it
fails, I would like to open a "/dev/null" filehandle as it were...

So, if it fails, I want this to produce no output:

print $filehandle "This should go nowhere\n";

No warnings would be nice too :) What's the best way to do this?

--Peter
 
A

A. Sinan Unur

(e-mail address removed) wrote in
Hi,

I want to be able to attempt to open a file handle on a file, and if
it fails, I would like to open a "/dev/null" filehandle as it were...

So, if it fails, I want this to produce no output:

print $filehandle "This should go nowhere\n";

No warnings would be nice too :) What's the best way to do this?

What have you tried so far?

Is the 'obvious' solution good enough for you?

#!/usr/bin/perl
use strict;
use warnings;

use constant NULLFILE => 'NUL';

my $file;
open $file, '>', 'Z:/Test'
or open $file, '>', NULLFILE
or die "Cannot open: $!";

print $file "Hello World\n";

__END__

Sinan
 
X

xhoster

Hi,

I want to be able to attempt to open a file handle on a file, and if it
fails, I would like to open a "/dev/null" filehandle as it were...

So, if it fails, I want this to produce no output:

Printing to a filehandle which is not open will produce no output, other
than optional warnings.
print $filehandle "This should go nowhere\n";

No warnings would be nice too :) What's the best way to do this?

Put
no warnings "unopened";

In the largest scope(s) which have all of the prints to this particular
file handle, but no prints to other file handles which you do want warnings
for.

Xho
 
B

Babacio

Hi,

I want to be able to attempt to open a file handle on a file, and if it
fails, I would like to open a "/dev/null" filehandle as it were...

So, if it fails, I want this to produce no output:

print $filehandle "This should go nowhere\n";

No warnings would be nice too :) What's the best way to do this?

Have a look on IO::Null, which allows you to link a file handle to a
bottomless sink.

use IO::Null;

open FILE, '>blop.txt' or tie(*FILE,IO::Null);
print FILE "Coucou\n";

This should work. A more elegant way to write it would be to use
IO::Null->new() but it seems that the package is quite buggy. Have a
look on the source and on a chapter about 'tie' in a perl book if you
whish to write something better.
 
P

peterkayatwork

Thanks - this should suffice for me. I'm not on a unix system, so I
have no real /dev/null to write to...

But I figured out it's not "unopened" that I need, but "closed":

#!/usr/bin/perl
use warnings;
use strict;
no warnings "closed";

my ($f);

open($f, ">") or warn "Couldn't open nothing - won't print to it!\n";

print $f "No error message if 'no warnings \"closed\"'\n";

__END__

Thanks :)

--Peter
peterkayatwork (at) yahoo.com
 
P

peterkayatwork

You posted no code, this ain't a "write my code for me" service.

Heh - I posted no real code because there wasn't any code to post. It
was still a theoretical question, not a "write this for me." Besides,
I did post some code that didn't do what I want:

print $filehandle "This should go nowhere\n";

Unfortunately, it produces a warning message...
why send something no where at all?

Because I want to be able to print to the file handle without having to
worry about whether or not it opened successfully - I've got more than
one filehandle I'm printing to simultaneously. It would be more
troublesome to pull bad filehandles out of the array...

--Peter
 
P

peterkayatwork

Fred said:
You obviously have something even more devious in mind.

The real goal is to be able to parse a single log file in multilple
(and extensible) formats, so someone can see say, a markup with all the
details (and some extra ones added), or a filtered version with only
important information marked. Rather then read through the original
file multilple times, I want to handle all the formats line-by-line,
and output them simultaneously. Hence the array of file handles...
But I don't want to cut out an entire format if the output file can't
be opened, because I'm still interested in meta-data collected.

I'm going to go with an object representation that stores the
filehandles along with the filtering code; that way, I just loop
through the objects, pass them the log data and the object can deal
with its filehandle.

Unfortunately, it needs to run on Windows, so I have to worry about
file locking keeping output files from opening... I'd let it break if
it was just people write-protecting files :)

I don't know if that counts as "devious", but there it is!

--Peter
peterkayatwork (at) yahoo.com
 
P

peterkayatwork

What, are we going to grep some regex out of the file..

More or less. Except it's more complicated then a regex, and I want 3
or 4 different output files. And I want to be able to define a new way
to get output files on the fly.
We'll read the entire file, that single log file. We can slurp it,
if we dare, or we can read it line by line.

I'm a big fan of line-by-line. Works easier on my poor brain :)
6 (?) But I don't want to cut out an entire format if the output file can't
be opened, because I'm still interested in meta-data collected.

I gotta confess, you lost me there on the meta-data part. Meta
data of what? The failed open/read on a file handle? And to what extent? OTOH.....

Ah, the meta-data would be how many lines got output to each file. Or
rather, the number of lines that matched each condition.
That sounds great! I wish I knew more of OOP and perl.

Ha! Once you know more about OOP and Perl you start asking questions
like "How do I use a code-ref to an object in perl?"

No, seriously, read `perldoc perltoot` & you'll know enuf to at least
create/access objects in Perl. It's really a fabulous tutorial, and
fairly quick to read. And interesting, to boot.

--Peter
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top