handle to /dev/null?

Discussion in 'Perl Misc' started by peterkayatwork@yahoo.com, Sep 20, 2005.

  1. Guest

    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
     
    , Sep 20, 2005
    #1
    1. Advertising

  2. wrote in
    news::

    > 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
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Sep 21, 2005
    #2
    1. Advertising

  3. Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Sep 21, 2005
    #3
  4. Babacio Guest

    writes:

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

    --
    Bé erre hue ixe eu elle, Bruxelles.
     
    Babacio, Sep 21, 2005
    #4
  5. Guest

    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
     
    , Sep 21, 2005
    #5
  6. Guest

    > 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
     
    , Sep 21, 2005
    #6
  7. Guest

    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
     
    , Sep 22, 2005
    #7
  8. Guest

    wrote:
    > 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
     
    , Sep 26, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    27,284
    Mike Schilling
    Mar 29, 2006
  2. Bo Peng
    Replies:
    13
    Views:
    13,448
    Siemel Naran
    Jul 18, 2004
  3. Ron Peterson

    /dev/urandom vs. /dev/random

    Ron Peterson, Jan 7, 2005, in forum: C Programming
    Replies:
    21
    Views:
    1,585
    Keith Thompson
    Jan 13, 2005
  4. AC
    Replies:
    0
    Views:
    188
  5. Guest
    Replies:
    2
    Views:
    165
    Bob Barrows [MVP]
    Sep 18, 2005
Loading...

Share This Page