Solved

How to pass a parameter?

Posted on 2000-02-22
14
233 Views
Last Modified: 2010-03-05
I have a Perl file wich call another perl file

I'am doing like this in Perlfile1.pl:

do 'PerlFile2.pl'

I need to pass a parameter to PerlFile2
How should I do this?
I also need to know how I recive it in PerlFile2 the parameter.

Something like this Example is what I need:

Perlfile1--------------
$var = value;
do 'PerlFile2?$var';


Perfile2-------------

$parameter=$var;
.......

0
Comment
Question by:Pampa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +2
14 Comments
 
LVL 1

Expert Comment

by:olthoff
ID: 2546562
system("perlfile2.pl $var");

in perlfile2.pl
$parameter = $ARGV[0];
0
 
LVL 1

Author Comment

by:Pampa
ID: 2546610
I'am using NT so I don´t wnat to call
"perl perlfile2.pl"
So I need to know the way to doing using the "do" command.

0
 
LVL 3

Expert Comment

by:guadalupe
ID: 2546672
do 'PerlFile2 $var';
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:maneshr
ID: 2546784
try this....


NOTE: The html calls first.pl, but first in turn passes the params to second.pl

You can change first.pl to do some processing and then pass the results to second.pl using the sample below.

==================test.html
<form method=post action="/cgi-bin/first.pl">
  <input type=text name=user_name><P>
  <input type=radio name=sex value=male>Male<BR>
  <input type=radio name=sex value=female>Female<P>
  <input type=submit value="Log It!!">
  </form>


==================first.pl
#!/usr/local/bin/perl

use CGI;
use URI::Escape;

$query=new CGI;

$name=$query->param('user_name');
$sex=$query->param('sex');

if ($query->param){   ##  Called by a HTML form
  $qstr= uri_escape(("/cgi-bin/second.pl?name=".$name."&sex=".$sex));

  print "Location: $qstr\n\n";  ## Re-direct to other PERL script
}


==========================second.pl
#!/usr/local/bin/perl

use CGI;
use URI::Escape;

$query=new CGI;

$name=uri_unescape($query->param('name'));  ##  Remove the special chars
$sex=uri_unescape($query->param('sex'));  ##  Remove the special chars

print "Content-type: text/html\n\n";
if ($query->param){   ##  Called by a HTML form

  print "First.pl has passed me <B>$name</B> & <B>$sex</B>\n";
}
0
 
LVL 1

Author Comment

by:Pampa
ID: 2546886
Maneshr, this method I know and it works, but what I need is more simple. I nedd to use the "do" command, like I explain before.

Guadalupe your comment doesn´t works ¿did you try before...?

0
 
LVL 1

Author Comment

by:Pampa
ID: 2547186
Now I'am trying this:

PerlFile1.pl
-------------

$var = NameofFile2;
system("perl $var");

This also doesn´t works, It´s seems it doesn't recognize $var inside "".
Any idea why?
 






0
 
LVL 1

Expert Comment

by:olthoff
ID: 2547235
You should include the path of the perl file.
$var = 'c:/dir1/perlfile.pl';
0
 
LVL 3

Expert Comment

by:guadalupe
ID: 2547266
It worked for me... try this:

#The inverted slash is on purpose!!
$path = "c:/script.pl";


system("perl $path $var");


0
 
LVL 16

Expert Comment

by:maneshr
ID: 2547310
try this...............


==================first.pl
#!/usr/local/bin/perl

use CGI;
use URI::Escape;

$query=new CGI;

$name=$query->param('user_name');
$sex=$query->param('sex');

print "Content-type: text/html\n\n";
if ($query->param){   ##  Called by a HTML form

  ## Passed the variables as one single string with ## as  the delimiter between successive variables.
  $qstr= uri_escape($name)."##".uri_escape($sex);

  `/www/sbarney/cgi-bin/second.pl $qstr`; ## Full path to other PERL script
  print "Done!!\n";
}

===================================second.pl

#!/usr/local/bin/perl

## Just to confirm i am writing the parameter to a file with ## as the delimiter between each field
open(TMP,">/tmp/manesh") || die $!;
print TMP $ARGV[0],"\n"; ## Receive you paramter passed by first.pl
close(TMP);
0
 
LVL 3

Accepted Solution

by:
monas earned 85 total points
ID: 2547443
Pampa,

      You could pass it only via global variables - variables with the same name. So, if you assign 5 to $a in your first.pl, and then "

do "second.pl"

and print $a in second.pl you will get 5. If you want more like procedural approach - to be able to pass values from different variables - you could construct subroutine. All the code looks like this:

File one.pl:
---------
#!/usr/local/bin/perl

$a=5;

sub dodo{
  local $a = $_[0];
do "two.pl";
}

dodo(1);
dodo(3);

File two.pl:
--------
print ">".$a."<\n";


Result:
-------
>1<
>3<
0
 
LVL 1

Author Comment

by:Pampa
ID: 2550366
I send part of my code because I think this is the problem and no the "Do" command.
This code is part of a logon scripta of a user in a NT plataform.

PerlFile1----------

$usuario=getdirs();
$username = "\\\\leda\\netlogon\\users\\".$usuario. ".pl";

If I print $username is all rigth

do "$username";  

This is never execute.....


I also try :
system(" %0\\..\\perl\\perl.exe -I%0\\..\\perl $username");

But this also never works....

Any ideas?



0
 
LVL 16

Expert Comment

by:maneshr
ID: 2550597
i created this sample program on my NT PC and it worked fine.
i would suggest that you modify and try executing the same.


make a sample PERL script called pampa.pl and put the foll in it.

===============pampa.pl

## Change this to any local directory first
## & then try with a network directory.

`dir c:\\temp > c:\\temp\\dirs`;


Then create main.pl
===============main.pl
$usuario="pampa";

$username = "c:\\temp\\".$usuario. ".pl";

print $username,"\n";

do "$username";


Check the c:\temp directory for the dirs file. if it has been created this program works fine. Then try with a network drive.


Hope that helps.
0
 
LVL 1

Author Comment

by:Pampa
ID: 2550635
Ok maneshr
I think the problem is taht the "Do" command doesn´t accept //name_server
and only accept local paths.


0
 
LVL 3

Expert Comment

by:guadalupe
ID: 2551300
It definitly does not accept "calls" to remote hosts unless you do something explicit like:

`ssh machine "perl /path/script.pl"`

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question