in reply to how can i send data to /dev/null ?

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

Replies are listed 'Best First'.
Re^2: how can i send data to /dev/null ?
by xdg (Monsignor) on Dec 11, 2005 at 13:44 UTC

    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.

Re^2: how can i send data to /dev/null ?
by jcc (Sexton) on Dec 12, 2005 at 16:37 UTC
    /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