#1
  1. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,382
    Rep Power
    594

    Odd output from open with pipe


    I am using the following code to launch a script and capture the output to a file.
    Code:
    open($pipe,"$helper_utils/collect_user_metrics.pl $url |") || die("Cannot run collect_user_metrics.pl: ".$!);
    open($fh,'>',"$tempoutput/collect_user_metrics.txt");
    print $fh while <$pipe>;
    close($pipe);
    close($fh);
    However, when this is executed I get the following output from my script (not in the capture file):

    GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01 d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB( 0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4) GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01 d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)GLOB(0xa01d5b4)

    If I comment out the above code, this output goes away and if I run the called script from the command line there is no such output, so clearly it is caused by the pipe. What is this and how do I stop it? TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    You could try this and see if it works

    Code:
    print {$fh} while <$pipe>;
    Your way is just printing the value of $fh which is GLOB(0xa01d5b4) on your system/exe.

    My bad that won't work. Its better if you select the output file before you print to it...Like this.

    Code:
    #!/usr/bin/perl
    
    use warnings;
    use strict;
    use feature qw/state/;
    use autodie qw/open close/;
    
    print "Enter input filenme->";
    chomp(my $ifilename = <STDIN>);
    
    open(my $IFILE, "<", $ifilename);
    
    print "Enter output filename->";
    chomp(my $ofilename = <STDIN>);
    
    open(my $OFILE, ">", $ofilename);
    
    select $OFILE;
    
    print  while ( <$IFILE> );#now print sends data to default which is $OFILE
    
    close($IFILE);
    close($OFILE);
    
    __END__
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    Try it like this:
    Code:
    open( METRICS, ">$tempoutput/collect_user_metrics.txt" );
    open( RESULTS, "$helper_utils/collect_user_metrics.pl $url |" )
     or die "Error running($helper_utils/collect_user_metrics.pl $url): $!\n";
    while ( <RESULTS> ) {
      print METRICS "$_";
    }
    close METRICS;
    close RESULTS;

    Comments on this post

    • gw1500se agrees
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,382
    Rep Power
    594
    Thanks for the replies. Spacebar208: I opted for your suggestion and that seems to work.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo