portable /dev/null ?

Discussion in 'Perl Misc' started by Larry, Jun 29, 2007.

  1. Larry

    Larry Guest

    I want to run an external command using "system" but discard its
    standard output. I know under *nix I could do:

    system 'myCmd > /dev/null';

    and Windows:

    system 'myCmd > nul';

    but is there a more portable way to do that?
     
    Larry, Jun 29, 2007
    #1
    1. Advertising

  2. Larry wrote:
    > I want to run an external command using "system" but discard its
    > standard output. I know under *nix I could do:
    >
    > system 'myCmd > /dev/null';
    >
    > and Windows:
    >
    > system 'myCmd > nul';
    >
    > but is there a more portable way to do that?


    If you would drop your requirement of using system(), then you could use
    backticks and capture (and then discard) the output in your Perl program.

    jue
     
    Jürgen Exner, Jun 29, 2007
    #2
    1. Advertising

  3. Larry

    Larry Guest

    On Jun 29, 11:03 am, "Jürgen Exner" <> wrote:
    > Larry wrote:
    > > I want to run an external command using "system" but discard its
    > > standard output. I know under *nix I could do:

    >
    > > system 'myCmd > /dev/null';

    >
    > > and Windows:

    >
    > > system 'myCmd > nul';

    >
    > > but is there a more portable way to do that?

    >
    > If you would drop your requirement of using system(), then you could use
    > backticks and capture (and then discard) the output in your Perl program.
    >
    > jue


    I thought of the backticks method, but I remember that being a problem
    in Windows the last time i tried it. Also, even if backticks works, I
    would prefer an option that discarded the output immediately, so that
    memory is not wasted if the output is large.
     
    Larry, Jun 29, 2007
    #3
  4. Larry schrieb:

    > On Jun 29, 11:03 am, "Jürgen Exner" <> wrote:
    >
    >>Larry wrote:
    >>
    >>>I want to run an external command using "system" but discard its
    >>>standard output. I know under *nix I could do:

    >>
    >>> system 'myCmd > /dev/null';

    >>
    >>>and Windows:

    >>
    >>> system 'myCmd > nul';

    >>
    >>>but is there a more portable way to do that?


    Maybe you're looking for File::Spec->devnull() ?

    >>
    >>If you would drop your requirement of using system(), then you could use
    >>backticks and capture (and then discard) the output in your Perl program.
    >>
    >>jue

    >
    >
    > I thought of the backticks method, but I remember that being a problem
    > in Windows the last time i tried it.


    perl -e "print `echo foo > nul`"
    works fine here.

    HTH,
    Andreas Pürzer
    --
    Have Fun,
    and if you can't have fun,
    have someone else's fun.
    The Beautiful South
     
    Andreas Pürzer, Jun 29, 2007
    #4
  5. Larry

    Paul Lalli Guest

    On Jun 29, 10:57 am, Larry <> wrote:
    > I want to run an external command using "system" but discard its
    > standard output. I know under *nix I could do:
    >
    > system 'myCmd > /dev/null';
    >
    > and Windows:
    >
    > system 'myCmd > nul';
    >
    > but is there a more portable way to do that?


    IIRC, the IO::All module has a devnull() method. You might want to
    look into it. The module is available on the CPAN.

    Paul Lalli
     
    Paul Lalli, Jun 29, 2007
    #5
  6. Larry

    Sisyphus Guest

    "Larry" <> wrote in message
    news:...
    >I want to run an external command using "system" but discard its
    > standard output. I know under *nix I could do:
    >
    > system 'myCmd > /dev/null';
    >
    > and Windows:
    >
    > system 'myCmd > nul';
    >
    > but is there a more portable way to do that?
    >


    You could use File::Spec. Then the code becomes (untested):

    my $devnull = File::Spec->devnull();
    system("myCmd > $devnull"); # should be portable

    Cheers,
    Rob
     
    Sisyphus, Jun 30, 2007
    #6
    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. Eli Bendersky
    Replies:
    1
    Views:
    1,172
    Mike Treseler
    Mar 1, 2006
  2. Bo Peng
    Replies:
    13
    Views:
    13,324
    Siemel Naran
    Jul 18, 2004
  3. Nicolas Fleury

    Portable /dev/null?

    Nicolas Fleury, Jan 27, 2004, in forum: Python
    Replies:
    3
    Views:
    308
    Peter Hansen
    Jan 28, 2004
  4. Replies:
    7
    Views:
    921
  5. Brendan Miller

    portable /dev/null

    Brendan Miller, May 2, 2008, in forum: Python
    Replies:
    6
    Views:
    336
    Brendan Miller
    May 2, 2008
Loading...

Share This Page