Solved

How to pass a parameter?

Posted on 2000-02-22
14
218 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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.…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

808 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