• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

How to pass a parameter?

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
Pampa
Asked:
Pampa
  • 5
  • 3
  • 3
  • +2
1 Solution
 
olthoffCommented:
system("perlfile2.pl $var");

in perlfile2.pl
$parameter = $ARGV[0];
0
 
PampaAuthor Commented:
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
 
guadalupeCommented:
do 'PerlFile2 $var';
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
maneshrCommented:
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
 
PampaAuthor Commented:
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
 
PampaAuthor Commented:
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
 
olthoffCommented:
You should include the path of the perl file.
$var = 'c:/dir1/perlfile.pl';
0
 
guadalupeCommented:
It worked for me... try this:

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


system("perl $path $var");


0
 
maneshrCommented:
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
 
monasCommented:
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
 
PampaAuthor Commented:
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
 
maneshrCommented:
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
 
PampaAuthor Commented:
Ok maneshr
I think the problem is taht the "Do" command doesn´t accept //name_server
and only accept local paths.


0
 
guadalupeCommented:
It definitly does not accept "calls" to remote hosts unless you do something explicit like:

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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now