Perl: Run perl script in the current process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Run perl script in the current process
# 1  
Old 12-07-2005
Perl: Run perl script in the current process

I have a question regarding running perl in the current process.

I shall demonstrate with an example.

Look at this.

Code:
sh-2.05b$ pwd
/tmp
sh-2.05b$ cat test.sh
#! /bin/sh
cd /etc
sh-2.05b$ ./test.sh 
sh-2.05b$ pwd
/tmp
sh-2.05b$ . ./test.sh 
sh-2.05b$ pwd
/etc
sh-2.05b$

So invoking ./test.sh spawns a sub-shell and runs the script. Whereas . ./test.sh will run the script in the current shell.

Now, how can I simulate the latter behavior with a perl script. I tried the following but it did not help.

Code:
sh-2.05b$ pwd
/tmp
sh-2.05b$ cat test.pl
#! /usr/bin/perl
chdir("/etc");
sh-2.05b$ ./test.pl 
sh-2.05b$ pwd
/tmp
sh-2.05b$ . ./test.pl 
sh: ./test.pl: line 2: syntax error near unexpected token `"/tmp"'
sh: ./test.pl: line 2: `chdir("/tmp");'
sh-2.05b$

Is there any other way of running the script to make sure that the changes made by the script will affect the current process ?

Thanks,
Vino
# 2  
Old 12-07-2005
AFAIK perl always creates a child.

Is there a reason you can't read the other script and do what it does in the context of the current process? ie., find the chdir and then do what it does in your perl script?
# 3  
Old 12-07-2005
Quote:
Originally Posted by jim mcnamara
Is there a reason you can't read the other script and do what it does in the context of the current process?
Yes, it can be done that way. That would work.

I am curious to know if it can be done this way.

Thanks,
vino
# 4  
Old 12-07-2005
Just a guess, but I think the . command is instructing the current shell to interpret the script - which it can't because it has perl commands/functions in it.
# 5  
Old 12-07-2005
The "exec" builtin in bash (sh on Linux seems to be too) will run the perl process without creating a new process by replacing the original shell process, just like the C exec* family of functions.

So you ought to be able to run a script by "./script.sh" to create a new process and then "exec perl script.pl" to continue execution without introducing a new process, but the original shell script will terminate (control will not return to the shell script).

Last edited by cbkihong; 12-07-2005 at 09:52 PM..
# 6  
Old 12-07-2005
Quote:
Originally Posted by cbkihong
The "exec" builtin in bash (sh on Linux seems to be too).....
On most (recent) Linux distros sh is soft/hard linked to bash...
Code:
# ls -l /bin/sh /bin/bash
-rwxr-xr-x  1 root root 616312 Dec  7  2004 /bin/bash
lrwxrwxrwx  1 root root      4 May 18  2005 /bin/sh -> bash

# 7  
Old 12-08-2005
Quote:
Originally Posted by cbkihong
The "exec" builtin in bash (sh on Linux seems to be too) will run the perl process without creating a new process by replacing the original shell process, just like the C exec* family of functions.

So you ought to be able to run a script by "./script.sh" to create a new process and then "exec perl script.pl" to continue execution without introducing a new process, but the original shell script will terminate (control will not return to the shell script).
Hmm...okie. So I used exec. This is what I got.

Code:
sh-2.05b$ pwd
/tmp
sh-2.05b$ cat test.sh
#! /bin/sh
exec perl /tmp/test.pl
sh-2.05b$ cat test.pl
chdir("/etc");
sh-2.05b$ ./test.sh
sh-2.05b$ pwd
/tmp
sh-2.05b$

I did a . ./test.sh and my xterm window just disappeared.

I changed test.pl to contain

Code:
#! usr/bin/perl
chdir("/etc");

and test.sh to
Code:
#! /bin/sh
exec /tmp/test.pl

The pwd still shows /tmp.

vino
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Programming

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... (1 Reply)
Discussion started by: alex_5161
1 Replies

3. Shell Programming and Scripting

Perl system() to run a script

Hello, I'm trying to run "csso" (minify css) in a CGI script from the web panel. That is not working: Returns error 0; my $cmd = qq`csso stylesheet.css > stylesheet.min.css`; system($cmd); But that is working: my $cmd = qq`echo 'blabla' > stylesheet.min.css`; system($cmd); I'm... (12 Replies)
Discussion started by: madispuk
12 Replies

4. Shell Programming and Scripting

current date - 8 months in perl script

I have a requirement as follows. when i pass a date to the perl script, it has to calculate the current date - 8 months and output the date back to the shell script in date format (YYYY-MM-DD). Current date - 8 months is not constant.. because leap year, and the months jan, mar, may,.... has... (4 Replies)
Discussion started by: kmanivan82
4 Replies

5. Shell Programming and Scripting

run remote perl script

Hello i want create perl script to connect remotely to another machine and run perl script please note that the remote script check for different variables remotely and not located on local machine also i want to know how to send interactive variables i am trying to make script to... (2 Replies)
Discussion started by: mogabr
2 Replies

6. Shell Programming and Scripting

run sql query via perl script

Hello, If I run this command on the server it works. # dbc "update config set radio_enabled = 0;" how can I execute the same command in perl. I have defined the dbc path. Can any one please correct the last line. #!/usr/bin/perl #database path $dbc='/opt/bin/psql -Userver... (0 Replies)
Discussion started by: sureshcisco
0 Replies

7. Shell Programming and Scripting

How to process and run a program in the background in perl?

Hi, I have a query about processing and running Perl program at the background. I have HTML file called Userform.html which accepts input from the user. As soon as input is given the contol goes to get.cgi (get.cgi does some processing and computing tasks). Actually get .cgi takes more... (0 Replies)
Discussion started by: vanitham
0 Replies

8. UNIX for Dummies Questions & Answers

Not able to run the simple perl script

Hi Experts, I have written simple perl script, which assign the value to variable and print it. Following is the script: $ cat 3.pl #!/usr/bin/env ksh #!/usr/bin/perl print "Hello World"; $iputlne = 34; print $iputlne; The error output is: $ /usr/bin/env perl 3.pl Hello World... (9 Replies)
Discussion started by: Amey Joshi
9 Replies

9. Shell Programming and Scripting

how to let the perl script to run for each file

Hi, I have more than 1 files in the directory. In bash, I can use cd /work for x in `ls` do : : done to run for each file in the directory. How about in perl script? filepath="ABC1" open(FILE, $filepath) or die "$filepath cannot be opened."; while(<FILE>) { : (1 Reply)
Discussion started by: natalie23
1 Replies

10. UNIX for Dummies Questions & Answers

cronjob to run perl script

Hi all Recently i had finished a perl script. When i run manually, the script work fine. But when i wanted to put the script in cron, it didn't get the same output as it run manually. I felt that it only execute the script until certain line then it stop as i see most of the related files didn't... (6 Replies)
Discussion started by: AirWalker83
6 Replies
Login or Register to Ask a Question

Featured Tech Videos