CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
2>&1 not working in backticks

 



Jeff29
Novice

Dec 23, 2010, 10:53 PM

Post #1 of 6 (1108 views)
2>&1 not working in backticks Can't Post

Hi,

I am using a "p4 where" command in one of my scripts,when the command fails I see the following "File(s) not in client view" messages getting printed on the command prompt from where the command is ran,Now I am trying to redirect this and capture to a variable "$p4where_output" but that doesnt seem to be happening.Can the perl gurus advise on how to capture STDERR to a variable?

STDERR OUTPUT:-

//Data/file.c - file(s) not in client view.

Code
	foreach my $file (@changed_files) 
{

my $p4where_output=`my $p4where_output=`p4 where $file > NUL: 2>&1`;
print "P4 where output:$p4where_output\n";
if($p4where_output eq 'file(s) not in client view')#this is failing because "file(s) not in client view is not getting saved to $p4where_output
{
push @changed_paths,"$file\n";
}
}



(This post was edited by Jeff29 on Dec 23, 2010, 11:05 PM)


FishMonger
Veteran / Moderator

Dec 24, 2010, 5:32 AM

Post #2 of 6 (1098 views)
Re: [Jeff29] 2>&1 not working in backticks [In reply to] Can't Post

Please copy/paste your actual code instead of retyping it. The code you posted won't compile, so it couldn't have produced that error.

Instead of using backticks, have you looked at using IPC::Opem3?
http://search.cpan.org/~jesse/perl-5.12.2/ext/IPC-Open3/lib/IPC/Open3.pm


Jeff29
Novice

Dec 24, 2010, 9:24 AM

Post #3 of 6 (1090 views)
Re: [FishMonger] 2>&1 not working in backticks [In reply to] Can't Post

I did try the open3 way but I am getting the following error.I have been trying various ways but couldnt get anything working.Any help is truly appreciated

Can't locate object method "p4" via package "where" (perhaps you forgot to load
"where"?) at stderr.pl line 25 (#1)
(F) You said to do (or require, or use) a file that couldn't be
found. Perl looks for the file in all the locations mentioned in @INC,
unless the file name included the full path to the file. Perhaps you
need to set the PERL5LIB or PERL5OPT environment variable to say where
the extra library is, or maybe the script needs to add the library name
to @INC. Or maybe you just misspelled the name of the file. See
perlfunc/require and lib.

Uncaught exception from user code:
Can't locate object method "p4" via package "where" (perhaps you forgot to load "where"?) at stderr.pl line 25.



Code
#!/usr/bin/perl -w  
use strict;
use warnings;
use diagnostics;
use Data::Dumper;
use Storable;
use Getopt::Std;
use File::Glob ();
use Cwd;
#use diagnostics;
use IPC::Open3;
my @changed_paths;
my @changed_files=("//perl/tools/files/data.c");
foreach my $file (@changed_files)
{
$file =~ s/#\d+$//;
print "FILE:$file\n";
#my $p4where_output=`p4 where $file > NUL: 2>&1`;
#print "P4 where output:$p4where_output\n";

my $cmd = "p4 where $file";

local(*HIS_IN, *HIS_OUT, *HIS_ERR);

my $childpid = open3(*HIS_IN, *HIS_OUT, *HIS_ERR, p4 where $cmd);
print HIS_IN "stuff\n";
close(HIS_IN); # Give end of file to kid.
my @outlines = <HIS_OUT>; # Read till EOF.
my @errlines = <HIS_ERR>; # XXX: block potential if massive
print "STDOUT:\n", @outlines, "\n";
print "STDERR:\n", @errlines, "\n";
print @errlines;
close HIS_OUT;
close HIS_ERR;
waitpid($childpid, 0);
if ($?) {
print "That child exited with wait status of $?\n";
}

if(@errlines eq 'file(s) not in client view')
{
push @changed_paths,"$file\n";
}
print @changed_paths;
}



rovf
Veteran

Dec 27, 2010, 7:01 AM

Post #4 of 6 (1061 views)
Re: [Jeff29] 2>&1 not working in backticks [In reply to] Can't Post

2>&1 works for me:

>perl -lwe "my $x=qx(ls does_not_exist 2>&1); print qq(Answer is: $x)"
Answer is: ls: does_not_exist: No such file or directory

>perl --version

This is perl, v5.10.0 built for MSWin32-x86-multi-thread


FishMonger
Veteran / Moderator

Dec 27, 2010, 8:59 AM

Post #5 of 6 (1059 views)
Re: [Jeff29] 2>&1 not working in backticks [In reply to] Can't Post

IPC::Open3 requires you to jump through a couple hoops which causes problems until you understand how it works.

So, lets take a step back and use a similar module which is easier to use and understand.

Here's a simple example test script.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use IPC::Run3;
use Data::Dumper;

my $cmd = [ qw(dir C:/bad) ];
my ($stdin, $stdout, $stderr);

run3($cmd, \$stdin, \$stdout, \$stderr);

print Dumper ($cmd, $stdin, $stdout, $stderr);


which outputs:

Code
D:\perl>perl-3.pl 
$VAR1 = [
'dir',
'C:/bad'
];
$VAR2 = undef;
$VAR3 = '';
$VAR4 = 'dir: C\\:/bad: No such file or directory
';


Note that using references when you run the comand is important in order to capture the data in the vars.


Jeff29
Novice

Jan 4, 2011, 4:38 PM

Post #6 of 6 (1025 views)
Re: [FishMonger] 2>&1 not working in backticks [In reply to] Can't Post

Hi,

If I run the "p4 where" command on a variable like "p4 where $file",I dont see any output where as if I change the command to run on a file like "p4 where //depot/perl/files/scripts/file.c",the command works,Can someone advise where am I going wrong?

Code
use strict;  
use warnings;
use IPC::Run3;
use Data::Dumper;
my @branched_paths;
my $savefile='//depot/perl/files/scripts/file.c';

my $cmd = [ qw(p4 where $savefile) ];
#my $cmd = [ qw(p4 where //depot/perl/files/scripts/file.c) ]; #works
#print "CMD:$cmd\n";
my ($stdin, $stdout, $stderr);

run3($cmd, \$stdin, \$stdout, \$stderr);

print $stderr;
#chomp($stderr);
if ($stderr =~ /file\(s\) not in client view/)
{
#print "in";
push @branched_paths,"$savefile";
}

print @branched_paths;



(This post was edited by Jeff29 on Jan 4, 2011, 4:40 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives