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:
my parameters won't pass through a pipe

 



bishop
Novice

Jul 29, 2009, 6:28 PM

Post #1 of 17 (1791 views)
my parameters won't pass through a pipe Can't Post

Hi everyone.

I am trying to pass some parameters from one process (test.pl) to a new process (test2.pl) using open() and a pipe.

But the parameters seem to not be getting passed.

Here is the code:

test.pl:


Code
my $PARAMETERS="parameters"; 

open (OUTPUT,"test2.pl '$PARAMETERS'|");
my $testresult=<OUTPUT>;
close (OUTPUT);

print $testresult;



test2.pl:


Code
print "argv[0]: $argv[0]";




output from running test1.pl:


Code
argv[0]:


(I want "argv[0]: parameters".)

Can anyone tell me what I am doing wrong?

Thanks,
Bishop


FishMonger
Veteran / Moderator

Jul 29, 2009, 7:26 PM

Post #2 of 17 (1789 views)
Re: [bishop] my parameters won't pass through a pipe [In reply to] Can't Post

Please post the actual code you're testing.

There are a number of issues with the code you posted, but since it's not a true representation of what you tested, it's meaningless to us.

Please post a short but complete script that demonstrates the problem and include any/all warnings and errors that it generates.

In the process of preparing that script, you will probably discover the problem and solution.


bishop
Novice

Jul 29, 2009, 7:35 PM

Post #3 of 17 (1787 views)
Re: [FishMonger] my parameters won't pass through a pipe [In reply to] Can't Post

The code I posted is copy-pasted directly from the two Perl code files I am testing, test.pl and test2.pl. It is the only code in the files. The two files are in the same directory.

For example, on the command line I change to that directory and I run

test.pl

and the output is

argv[0]:

when I expect

argv[0]: parameters

I'm not sure what you mean by it not being a true representation of what I tested. Do you want me to upload the actual files or something?

Thanks,
Bishop


FishMonger
Veteran / Moderator

Jul 29, 2009, 8:28 PM

Post #4 of 17 (1784 views)
Re: [bishop] my parameters won't pass through a pipe [In reply to] Can't Post

EVERY Perl script you write should include the warnings and strict pragmas.

Code
use strict; 
use warnings;


Case matters in Perl
$argv[0] should be $ARGV[0]


bishop
Novice

Jul 29, 2009, 9:06 PM

Post #5 of 17 (1782 views)
Re: [FishMonger] my parameters won't pass through a pipe [In reply to] Can't Post

Ok thanks. That helped a little.

I tried

test.pl:

Code
use strict; 
use warnings;

my $PARAMETERS="parameters";

open (OUTPUT,"test2.pl '$PARAMETERS'|");
my $testresult=<OUTPUT>;
close (OUTPUT);

print $testresult;


test2.pl:

Code
use strict; 
use warnings;

print "ARGV[0]: $ARGV[0]";


and now my output from running test.pl is:


Code
Use of uninitialized value $ARGV[0] in concatenation (.) or string at C:\...\test2.pl line 4. 
ARGV[0]:


...but why is $ARGV[0] uninitialized? Aren't I passing in the parameters?

I also tried running "test2.pl 'parameters'" and "test2.pl parameters" directly from the command line and get the same warning message output for each.

Also the same warning for $ARGV.

If I try printing $#ARGV I get -1.

Why aren't my perl scripts getting their parameters? Could it be something about my environment? I'm using Strawberry Perl 5 on Windows XP...

Thanks,
Bishop


FishMonger
Veteran / Moderator

Jul 30, 2009, 4:49 AM

Post #6 of 17 (1776 views)
Re: [bishop] my parameters won't pass through a pipe [In reply to] Can't Post

Do you have a file type association for the .pl extension?

Is the 'open' action configured correctly?

Quote
C:\Perl\bin\perl.exe "%1" %*

You will probably need to adjust the path.


bishop
Novice

Jul 30, 2009, 7:12 PM

Post #7 of 17 (1763 views)
Re: [FishMonger] my parameters won't pass through a pipe [In reply to] Can't Post

I realized my problem is a lot more basic than I was thinking in my original post. Even not considering the open action at all, my Perl scripts are not getting their arguments for some reason.

.pl File type is associated.

*.pl scripts execute fine from the command line.

But they don't get their arguments.

Example:

test.pl

Code
use strict; 
use warnings;

print "#ARGV: $#ARGV\n";
print "ARGV[0]: $ARGV[0]\n";


On the command line, I run:


Code
C:\test>test.pl arg1


Output is:


Code
#ARGV: -1 
Use of uninitialized value $ARGV[0] in concatenation (.) or string at C:\test\test.pl line 5.
ARGV[0]:


Like I said it is strawberry perl 5 on windows XP... have you ever heard of something like this? I've been reading through tons of pages on perl command line arguments they all seem to assume that $ARGV should just work...


FishMonger
Veteran / Moderator

Jul 30, 2009, 7:57 PM

Post #8 of 17 (1761 views)
Re: [bishop] my parameters won't pass through a pipe [In reply to] Can't Post

IMO, this one of the areas that shows that ActiveState is better than strawberry, because ActiveState correctly configures the .pl file extension.

Check you .pl file association and fix its action config. The key part is the "%1" %* after the path to Perl.

e.g.,

Code
C:\Perl\bin\perl.exe "%1" %*


http://support.microsoft.com/kb/307859


(This post was edited by FishMonger on Jul 30, 2009, 7:57 PM)


KevinR
Veteran


Jul 30, 2009, 8:31 PM

Post #9 of 17 (1758 views)
Re: [bishop] my parameters won't pass through a pipe [In reply to] Can't Post

I have the same problem on Vista but it works for me like this:


Code
use strict;  
use warnings;

my $PARAMETERS = "parameters";

open (OUTPUT,"perl c:/PerlTest/foo.pl $PARAMETERS|") or die "$!";
my $testresult = <OUTPUT>;
close (OUTPUT);

print $testresult;


Code for foo.pl:


Code
#!perl 
print "@ARGV";
exit;


Even though I have .pl set to open with perl (strawberry perl 5.10 on Vista 64 bit) it will not pass the parameters unless I include perl on the open line. Perl is in the command path so just using "perl" works. I can't find how to do what Fish suggests on Vista.
-------------------------------------------------


(This post was edited by KevinR on Jul 30, 2009, 8:33 PM)


KevinR
Veteran


Jul 30, 2009, 8:51 PM

Post #10 of 17 (1753 views)
Re: [KevinR] my parameters won't pass through a pipe [In reply to] Can't Post

I can change a program that opens a file but not specific attributes just using Vista. I found a utility that lets you change the file type association on Vista including such things as the action attributes of a file:

http://types.sourceforge.net/
-------------------------------------------------


KevinR
Veteran


Jul 30, 2009, 9:06 PM

Post #11 of 17 (1751 views)
Re: [KevinR] my parameters won't pass through a pipe [In reply to] Can't Post

Now that I have changed the .pl file association open action to:

"C:\strawberry\perl\bin\perl.exe" "%1" %*

It sort of works but will only pass one argument
-------------------------------------------------


bishop
Novice

Jul 30, 2009, 11:32 PM

Post #12 of 17 (1744 views)
Re: [KevinR] my parameters won't pass through a pipe [In reply to] Can't Post

Ok thank you Fish that worked. I got the association to work finally. I can pass arguments to test.pl and they print out as expected.

Now I can go back to what I thought was my original problem, passing parameters through pipes.

I have:

test.pl

Code
use strict; 
use warnings;

print "$ARGV[0]";

exit(0);


test2.pl

Code
use strict; 
use warnings;

my $PARAMETERS="KEY1=VAL1 KEY2=VAL2 KEY3=VAL3";

open (OUTPUT,"test.pl '$PARAMETERS'|");
my $testresult=<OUTPUT>;
close (OUTPUT);

print "\nquotes:$testresult\n";

open (OUTPUT,"test.pl $PARAMETERS|");
$testresult=<OUTPUT>;
close (OUTPUT);

print "\nno quotes:$testresult\n";

1;


The command:

Code
c:\test\test2.pl


Produces output:

Code
 
quotes:'KEY1=VAL1

no quotes:KEY1=VAL1


... when I was expecting:


Code
 
quotes:KEY1=VAL1 KEY2=VAL2 KEY3=VAL3

no quotes:KEY1=VAL1


...was I expecting the wrong thing? I couldn't find the right info about single quotes in a double quotish context after a few minutes on google so I'm not sure.

An interprocess parameter passing mechanism like this exists and seems to be working (on a linux machine running an old version of Perl in an old server software stack) in some legacy code I am dealing with, which is where I am getting these expectations. Maybe this is not the way to pipe parameters between perl processes anymore?


KevinR
Veteran


Jul 31, 2009, 10:48 AM

Post #13 of 17 (1735 views)
Re: [bishop] my parameters won't pass through a pipe [In reply to] Can't Post

Like I said in most post above, it sort of works but only passes one argument.

You might want to look at using IPC::Open2 and IPC::Open3, both of which come with perl.
-------------------------------------------------


(This post was edited by KevinR on Jul 31, 2009, 10:50 AM)


FishMonger
Veteran / Moderator

Jul 31, 2009, 11:03 AM

Post #14 of 17 (1732 views)
Re: [KevinR] my parameters won't pass through a pipe [In reply to] Can't Post


In Reply To
Like I said in most post above, it sort of works but only passes one argument.


No, it passes all of the arguments. The main problem is that test.pl specifically specifies to print only 1 of the arguments.

change

Code
print "$ARGV[0]";


to

Code
print $ARGV[2];  # print the 3rd arg


or

Code
print "@ARGV";


or

Code
print "$_\n" for @ARGV;



(This post was edited by FishMonger on Jul 31, 2009, 11:04 AM)


FishMonger
Veteran / Moderator

Jul 31, 2009, 11:11 AM

Post #15 of 17 (1728 views)
Re: [FishMonger] my parameters won't pass through a pipe [In reply to] Can't Post

I forgot to mention, in that last example, you also need to change the context when reading in the output.

i.e., change all occurrences of $testresult to @testresult


KevinR
Veteran


Jul 31, 2009, 11:20 AM

Post #16 of 17 (1726 views)
Re: [FishMonger] my parameters won't pass through a pipe [In reply to] Can't Post

OK, I tried a few things. In the script being called if I do this:


Code
print "@ARGV";


then I get back of course the stringified scalar of @ARGV which will be one string. If I do this:


Code
print "$_" for @ARGV;


I get the same thing. If I do this:


Code
print "$_\n" for @ARGV;


and in the other script I have to return the output to a list:


Code
my @testresult = <OUTPUT>;


then everything works as it should and now that I think about it, that's the way it should work. Returning the output of the script to a scalar will only get the first "line" back.

The called script has to print an output record separator and the calling script has to get the results back in a list or using "while".
-------------------------------------------------


(This post was edited by KevinR on Jul 31, 2009, 11:26 AM)


KevinR
Veteran


Jul 31, 2009, 11:23 AM

Post #17 of 17 (1723 views)
Re: [FishMonger] my parameters won't pass through a pipe [In reply to] Can't Post


In Reply To
I forgot to mention, in that last example, you also need to change the context when reading in the output.

i.e., change all occurrences of $testresult to @testresult


Yea, we were posting at the same time, see my last post.
-------------------------------------------------

 
 


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

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