469,268 Members | 920 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

Executing perl script inside perl script

rajiv07
141 100+
Hi to all,

I want execute a perl program inside another perl program with passing some
values to called program by calling program.

Is any Idea please.

Thanks in advance.
Aug 24 '07 #1
9 19606
numberwhun
3,503 Expert Mod 2GB
You can do it the same way you would call any other system command, use back tics. For example:

Expand|Select|Wrap|Line Numbers
  1. `perlscript.pl $option1 $option2`;
  2.  
Regards,

Jeff
Aug 24 '07 #2
rajiv07
141 100+
Thank for your reply,

I want execute the perl program with passing some parameter.

suppose perl1 execute perl2. but perl2 need some parameter value to execute.
How to pass parameter to perl2.

I have code

perl1.pl
----------
[PHP]#!/usr/bin/perl

exec("perl2.pl");[/PHP]

Here i want execute with some parameter passing to perl2.pl.


Is any idea please

Thanks .
Aug 24 '07 #3
numberwhun
3,503 Expert Mod 2GB
First, if you are executing a command and you expect to store any returned data for use in your script, then do not use the exec command as it executes but returns nothing.

Instead, use either the system command or back tics ( as I have showed you).

I have already told you how to pass the parameters to the perl script in my example. If you look at the example I gave you in my last post, it shows the Perl script being executed followed by two variables ($option1 and $option2). Call them $parameter1 and $parameter2 if you wish, but whatever values they contain would be passed to the perl script being called.

Regards,

Jeff
Aug 24 '07 #4
miller
1,089 Expert 1GB
If I'm executing another perl script, my tendency is to always use the do function.

Expand|Select|Wrap|Line Numbers
  1. {
  2.     local @ARGV = ($option1, $option2);
  3.     do 'perlscript.pl';
  4. }
  5.  
If you wish to have detailed error checking, there is an example included in the documentation on how to achieve that.

- Miller
Aug 25 '07 #5
rajiv07
141 100+
Thank u for Your reply,

How do i return the value to calling script.please could you explain with example.

Thank u.
Aug 25 '07 #6
rajiv07
141 100+
Hi to all ,

Here i have to file 1.pl and 2.pl. 2.pl is calling program and 1.pl called program.

2.pl (Calling Script)
------
system("1.pl","1","3");

1.pl(Called Script)
----
my($val1,$val2)=($ARGV[0],$ARGV[1]);

#do sum calculation
-----
----
---
Here How to return the calculated value to 2.pl.

Is any idea please

Thank U.
Aug 25 '07 #7
numberwhun
3,503 Expert Mod 2GB
That is actually not as hard as you think. If you take and assign your scripts execution (in back tics) to a variable, then the value outputted by that script is passed to that variable in the first script. I wrote up the following really quick as an example.

Here is my first script(test.pl), which is the main script calling the second script:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $rtn = `./test2.pl\n`;
  7.  
  8. print("$rtn");
  9.  
As you can see, this calls script 2 as the argument to the variable $rtn.

Here is the contents of test2.pl:

Expand|Select|Wrap|Line Numbers
  1. #!/opt/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $value = 2 + 2;
  7.  
  8. print("$value\n");
  9.  
This sets the value of $value to be 4 and prints it to the screen. The value 4 is what is then outputted to the screen by script 1.

Regards,

Jeff
Aug 25 '07 #8
rajiv07
141 100+
Thank you for your reply,

when i run this script it gives an error like"'.' is not recognized as an internal or external command,operable program or batch file.".

Please let me know what is the problem.
Aug 27 '07 #9
numberwhun
3,503 Expert Mod 2GB
Which OS are you on? If you are on Unix, then you have to have the shebang line (#!/usr/bin/perl) at the beginning of the script as the first line. If you are on Windows, then you need to execute your script as follows:

Expand|Select|Wrap|Line Numbers
  1. c:> perl ./scriptname.pl
  2.  
Hope that helps.

Regards,

Jeff
Aug 27 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Nick Coghlan | last post: by
15 posts views Thread by Nick Coghlan | last post: by
reply views Thread by Ian Warford | last post: by
3 posts views Thread by Nathan Gilbert | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.