Loading ...
Sorry, an error occurred while loading the content.

Re: How to capture the Output?

Expand Messages
  • Damien Carbery
    ... Ok, while I was a little surprised to see exec I assumed that this was the way things worked with the Test::Cmd module. From looking at the Test::Cmd
    Message 1 of 4 , Aug 5 3:18 AM
    • 0 Attachment
      > --- In perl-beginner@yahoogroups.com, "Damien Carbery"
      > <daymobrew@y...> wrote:
      > > --- In perl-beginner@yahoogroups.com, "vishal_kharge"
      > > <vishal_kharge@y...> wrote:
      > > > Hell All:
      > > >
      > > > I need to capture the output of the CMD.
      > > >
      > > > I have to test the 'sum'command of Unix with invalid option.
      > > >
      > > > Below is my code:
      > > >
      > > > use Test::Cmd;
      > > > ### Create output log file to write the test results ###
      > > > open(OUT,">Result.txt")||die "Can not open output file!";
      > > >
      > > > #Invalid Switch Option
      > > > $Switch = "-s&";
      > > >
      > > > #File Name
      > > > $FileName = "Data.txt";
      > > >
      > > > -sh;
      > > > exec ("sum $Switch $FileName");
      > > >
      > > > #Capture Error Output
      > > > @Error= $test->stderr()
      > > > print OUT "@Error \n";
      > > > close (OUT);
      > > >
      > > >
      > > >
      > > > *********************************
      > > > The actual output when the same command is run through shell is:
      > > > [guest@qdi IO]$ sum -st Data.txt
      > > > sum: invalid option -- t
      > > > Try `sum --help' for more information.
      > > > ***************************************
      > > >
      > > > How should I capture this output?
      > > >
      > > > Thanks in Advance,
      > > > Vishal Kharge
      > >
      > > Would sending the stderr to stdout help?
      > > exec ("sum $Switch $FileName 2>&1");
      --- In perl-beginner@yahoogroups.com, "vishal_kharge"
      <vishal_kharge@y...> wrote:
      > Hello,
      >
      > But this ouput is not Stdoutput as well as not a stderror, so how to
      > capture this?
      >

      Ok, while I was a little surprised to see 'exec' I assumed that this
      was the way things worked with the Test::Cmd module.
      From looking at the Test::Cmd docs
      (http://www.baldmt.com/Test-Cmd/Test-Cmd-1.05.html) it isn't.

      Forgetting about the Test::Cmd module for a moment, you use backticks
      to capture the output of a launched command:
      $captured_output = `sum $Switch $FileName 2>&1`;
      or
      @captured_output = `sum $Switch $FileName 2>&1`;
      The '@' version puts each output line into an element in the list.
      I use '2>&1' so that errors are captured too, otherwise they just go
      to the screen.

      From the docs (have you read them?) to have Test::Cmd capture the
      output it looks like you have to use '$test->run'.
    Your message has been successfully submitted and would be delivered to recipients shortly.