Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to execute commands in the same shell as Perl script

by bhagperl (Novice)
on Sep 13, 2011 at 11:18 UTC ( [id://925676]=perlquestion: print w/replies, xml ) Need Help??

bhagperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I want to run the shell script in the same process / OS shell as parent perl script. Idea is to run the shell script or any OS command in the same shell as Parent perl script.

Instead of forking a new process and run the command as done in "system" function.

As in UNIX we have "source" or "." commands to run the shell scripts in the same shell instead of child process.

Please suggest me some solutions for this in case of PERL.

Thanks in advance

  • Comment on How to execute commands in the same shell as Perl script

Replies are listed 'Best First'.
Re: How to execute commands in the same shell as Perl script
by cdarke (Prior) on Sep 13, 2011 at 12:21 UTC
    You seem to be confusing just what a shell is. A shell is just another program. So bash is a program that runs in a process, as is ksh, as is csh, as is ls, as is ps, as is perl, as is python, etc. Some shell commands are 'built-ins', like cd, and those do not require a separate process. Others that can be shell commands, which you might inaccurately call an "OS command", are programs like ls and ps which have nothing to do with a shell - it is just that we often invoke them from a shell.

    You can't directly 'source' the ls or ps programs for example, they are binary (ELF) executables that run in a separate process. You can source the command to invoke them, but they will still run in a different process to your shell. The perl program is no different.

    Fortunately there are Perl equivalents to many UNIX programs and utilities that you might call from a shell, see UNIX 'command' equivalents in Perl.
Re: How to execute commands in the same shell as Perl script
by Perlbotics (Archbishop) on Sep 13, 2011 at 11:33 UTC

    Seems, you want to run an external program with the same PID as the parent process?

    See exec if you want to terminate the current parent (perl-)process. Here, you can run any kind of program not limited to Perl programs (the original process is lost).

    See do (with filenames) and eval for options to run scripts within the same perl instance. However, that obviously doesn't work with non-Perl programs.

    BTW, what is PERL? ;-)

Re: How to execute commands in the same shell as Perl script
by chrestomanci (Priest) on Sep 13, 2011 at 13:11 UTC

    I think bhagperl might be attempting to write a perl script that alters his environment similar to the very common bash idiom of sourcing a script such as:

    [username@localhost]$ . ./set_envs.sh

    If you attempt something like that with a perl script it does not work because perl runs in a child process of the current shell, and any changes it makes to it's environment don't affect the calling process (the user's shell).

    The solution (under unix) is to use eval and backticks and have perl emit the commands to change the environment to stdout. eg:

    [username@localhost]$ eval `perl set_envs.pl`

    Where the perl script is something like:

    if( $ENV{'SHELL'} =~ m:/bash$: ) { printf 'export ENVAR=%s\n', $value; ... } elsif ( $ENV{'SHELL'} =~ m:/csh$: ) ...

      Hi All,

      Thanks for all those interpretations and series of analysis of what i meant :) Sorry folks i was not clear then.

      There are 2 Answers that i seek:

      1) Actually what i meant is, does a child process in PERL has access to Parent process's env variables.

      2) Can Child process change parent process's environment settings in PERL ?

      I want a child process to run some commands and then change parent process's env settings. Is this possible in PERL ?

      Please let me know if there is solution for this.

      Thanks !

      -bhag
        does a child process in PERL has access to Parent process's env variables

        Sigh. There ain't no PERL. The language is named Perl, and the executable that reads it is named perl.

        No, a child process can not access the environment of the parent, at least not in a portable way (i.e. without playing dirty tricks). Most of the times, the child process inherits a COPY of the environment, but in special cases, the childs environment may be manipulated by the parent before starting the child.

        Can Child process change parent process's environment settings in PERL ?

        No. This is the same question as before. Allowing that would be dangerous.

        I want a child process to run some commands and then change parent process's env settings.

        Make perl emit a shell script that can be sourced by the parent shell (after perl has exited) to change its environment. Or even better: Run the entire job from within Perl, so you don't have to mess with any shells.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: How to execute commands in the same shell as Perl script
by JavaFan (Canon) on Sep 13, 2011 at 13:02 UTC
    Idea is to run the shell script or any OS command in the same shell as Parent perl script.
    What do you mean by "in the same shell"? Perl programs don't run "in a shell". It maybe that their parent process is a shell, but that's not always the case. You cannot create another process that has the same parent as your parent, but you can use exec, which replaces the program you are currently running with another (and it's still the same program). If you want to continue the running program, you can fork, and then exec in the parent. This makes the "old" program a child of the "new" one, but the "new" one will have the shell (assuming the "old" one had a shell as its parent) as its parent.

    I'm kind of curious what you are trying to achieve though.

Re: How to execute commands in the same shell as Perl script
by pvaldes (Chaplain) on Sep 13, 2011 at 11:30 UTC
    $ perl myscript.pl | other commands | and more comands

    (Or ; or && or || instead |). Are you sure that this is what you want?

    Alternativelly, do with perl all the shell script work

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://925676]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found