perl_mystery has asked for the wisdom of the Perl Monks concerning the following question:

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.I am not sure if its due to run3 issue or something to with qw operator?Can someone advise where am I going wrong?

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;

Replies are listed 'Best First'.
Re: IPC::Run3 or qw operator issue?
by toolic (Bishop) on Jan 05, 2011 at 01:03 UTC
    The problem is with qw because it does not interpolate the $savefile variable. See Quote and Quote like Operators. You can prove this as follows:
    use strict; use warnings; use Data::Dumper; my $savefile='//depot/perl/files/scripts/file.c'; my $cmd = [ qw(p4 where $savefile) ]; print Dumper($cmd); print ref($cmd), "\n"; __END__ $VAR1 = [ 'p4', 'where', '$savefile' ]; ARRAY

    You could try this instead:

    my $savefile='//depot/perl/files/scripts/file.c'; my $cmd = [ (qw(p4 where), $savefile) ]; print Dumper($cmd); print ref($cmd), "\n"; __END__ $VAR1 = [ 'p4', 'where', '//depot/perl/files/scripts/file.c' ]; ARRAY
Re: IPC::Run3 or qw operator issue?
by ikegami (Patriarch) on Jan 05, 2011 at 01:19 UTC

    Sometimes, I use this notation:

    my $cmd = [ cmd => ( opt => $val, opt => $val ) ];

    In this case,

    my $cmd = [ p4 => ( where => $savefile ) ];

    It's probably overkill. Alternatives:

    my $cmd = [ p4 => 'where', $savefile ]; my $cmd = [ qw( p4 where ), $savefile ]; my $cmd = [ 'p4', 'where', $savefile ];