PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !


 
Thread Tools Search this Thread
Top Forums Programming PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !
# 1  
Old 02-28-2018
PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open())
I would like to run another PERL-script from first one, not exiting from the first one (as by exec()), not catching the STDOUT of the called script (as in the back tick processing,) but having it printed out, as in initial script (as it is by system().)
I do not need the return status (as by system()); but,
- I would like to have the called script to set some variables, that will be accessible in the calling script (sure by the our @set_var;)

My attempt (that I am not able to make do what I need) is:
Code:
...
 if($condition) 
{  local $0 = 'script2.pl';
    local @ARGV = ('first-arg', 'second_arg');
    do script2.pl;
}
print "set array is: '@set_var'\n";
...

The 'script2' would have something like:
Code:
#!/usr/bin/perl
...
  print "having input parameters: '@ARGV'\n";
  ... # all script activities
  our @set_var = ($val1, $val2, $val3);
  exit 0;

The proble in my code is that the do ... command is executing on beginning of the first script run and not in the place, where it is prepared for it (by set local .. vars!)

I did try to use the eval "do script2.pl" :
- now it is executed in the proper place, but it is not setting the @set_var into the first script process!

Is there any idea to do it as I would like to have it?
(I understand, that I can rewrite the script2.pl as a function and load it by require() and execute the function: that will do everything as I prefer it; but I would like to leave the second script as is to be executable from shell by itself, as it is now.)

Does anybody have an idea how to do my whim?
# 2  
Old 03-03-2018
The solution to my question is the command
do EXPR;
, having as EXPR the script-2!

That way the script-2 will have defined in script-1 some variables;
it will process everything, printing on STDOUT what it should print (the script-2), and
it will set any variable that will be available in the script-1 after completing the script-2.

After finishing the script-2 control coming back to script-1

That is all I been looking for!

(Hmm, after review my question, surprizingly realizing that I have used the 'do ..'
How it happening that I have mislead myself to have it not enough, I am not sure!
--- My apologizes for all that mess!!!! )
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Perl script with lock to execute only once in a day

Hi, I am new to perl and have a script to which i want to ensure that no matter how many ever times i execute the script it should execute only once per day. Cronjob is not a safe method as I want to built in capability inside the script. (1 Reply)
Discussion started by: ctrld
1 Replies

2. Shell Programming and Scripting

[perl] execute remotely script

Hello Can some help with write part of perl script I need something like this in perl SSH="/bin/ssh -o BatchMode=yes -o" USER="test" SRV="server" SCRIPT_TO_EXEC="/tmp/test.sh" -> shell script OUT=/tmp/out.file ${SSH} -l ${USER} ${SRV} 'sudo /usr/bin/ksh -s' < ${SCRIPT_TO_EXEC} >> ${OUT}... (1 Reply)
Discussion started by: vikus
1 Replies

3. Shell Programming and Scripting

Excuting perl script from within a perl script with variables.

Not sure what I am doing wrong here, but I can print the list with no issue. Just a blank screen with the 'do'. #!/usr/bin/perl open FILE, "upslist.txt"; while ($line=<FILE>){ if ($line=~/^(.*?),(.*?)$/){ #print "ups:$1 string:$2\n"; do 'check_snmp_mgeups-0.1.pl -H $1 -C $2'; } ... (1 Reply)
Discussion started by: mrlayance
1 Replies

4. Shell Programming and Scripting

Execute immediate in perl script

hi All, i have to modify a shell script written in Perl. i have to use execute immediate within this. i have to create a temporary table but it should have name like ar_data_$mmyyyy , how can i achieve this? any help on this would be highly appriciated. (6 Replies)
Discussion started by: lovelysethii
6 Replies

5. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Shell Programming and Scripting

Perl Script to execute todays date.

Hi Folks, I have created a script last month to retrive files thru FTP and cronjob was running fine till yesterday. But the naming convention of the daily file is Filename_<date>.xml where date is YYYYMMDD. But today i have received file name as Filename_20110232.xml :( Part of my Perl... (4 Replies)
Discussion started by: Sendhil.Kumaran
4 Replies

7. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

8. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

9. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

10. UNIX for Dummies Questions & Answers

a cron job needs a perl script to execute

Hello evreyone, this is my first post, and to say i'm new to this is an understatement. I know very little about perl scripts and hope some one can help me. i'm looking to get a script that a cron job can execute. what the script needs to to is 1) connect to a mysql database 2) go to a... (2 Replies)
Discussion started by: Exader
2 Replies
Login or Register to Ask a Question