Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello again, thanks for the last answer.

How can i send data to /dev/null ?

while(<F>){
# do nothing ?
}

Is it as simple as this ? surely not ?

thanks again,

abachus.

Replies are listed 'Best First'.
Re: how can i send data to /dev/null ?
by tirwhan (Abbot) on Dec 11, 2005 at 10:30 UTC

    Well, you can go through the explicit process of opening /dev/null and writing your data to it, but effectively you'd be doing the same thing, your data doesn't get written anywhere. This seems pretty senseless to me though, what are you trying to do? Delete a file? In that case look at perldoc -f unlink. If you're trying to empty a file, open that file for writing and then close it again

    open(my $null, ">","path/to/file") or die "Can't open file for writing +"; close $null or die "Can't close open filehandle";

    , but you have to be sure noone is going to try to write to the file while you're doing that, otherwise the outcome may not be what you want. For anything more complicated Tie::File may come in handy.


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

      If for some reason, someone wants to write to /dev/null and wants portability, it's good to use File::Spec

      open( my $null, ">", File::Spec->devnull ) or die "Couldn't open null device";

      I could see this potentially being useful if testing some code that takes a filehandle, but where one doesn't actually want to be writing anything to disk.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      /dev/null is a device, not a file. DO NOT try to open it for writing to as a file, especially if you're root. You don't want to overwrite the device link with a flat file.
        Hmm?
        sh-2.05b$ stat /dev/null File: `/dev/null' Size: 0 Blocks: 0 IO Block: 4096 character +special file Device: 303h/771d Inode: 246087 Links: 1 Device type: 1 +,3 Access: (0666/crw-rw-rw-) Uid: ( 0/ root) Gid: ( 0/ roo +t) Access: 2005-06-18 10:16:06.000000000 +0200 Modify: 2005-06-18 10:16:06.000000000 +0200 Change: 2005-06-18 10:16:06.000000000 +0200 sh-2.05b$ whoami root sh-2.05b$ perl -e'open($null,">","/dev/null")or die "foo";print $null +"something" or die "bar"; close $null' sh-2.05b$ stat /dev/null File: `/dev/null' Size: 0 Blocks: 0 IO Block: 4096 character +special file Device: 303h/771d Inode: 246087 Links: 1 Device type: 1 +,3 Access: (0666/crw-rw-rw-) Uid: ( 0/ root) Gid: ( 0/ roo +t) Access: 2005-06-18 10:16:06.000000000 +0200 Modify: 2005-06-18 10:16:06.000000000 +0200 Change: 2005-06-18 10:16:06.000000000 +0200

        This is on Linux. Are there *NIXes where this behaves differently?


        Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: how can i send data to /dev/null ?
by GrandFather (Saint) on Dec 11, 2005 at 10:35 UTC

    Here are a few other simple ways:

    1 while (<F>); 1 for (<F>); join '', (<F>); map {;} (<F>); () = <F>;

    :)


    DWIM is Perl's answer to Gödel
Re: how can i send data to /dev/null ?
by Anonymous Monk on Dec 11, 2005 at 10:57 UTC
    I'm planning to read data from a handle yes, but the
    incoming data is arbitrary in both content and length.

    After the first number of bytes read, should the program
    decide it no longer wishes to continue to read, i'd like the
    remaining data to be discarded.

    Thus I could just close the file i guess, amen.

    thanks

    abachus.