Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to pass a parameter?

Posted on 2000-02-22
14
Medium Priority
?
239 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 255 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

Technology Partners: 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!

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

609 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