1

Today, I write a bash script to open and close a special python virtual envionment. You want to close the virtual environment, you must in the same bash process with command deactivate, so you can use command ~source~ to execute you bash script.(Preface)

Perl question

Here is my perl script

#!/usr/bin/perl 
BEGIN{
    $\="";
}
use warnings;
use strict;
use feature "switch";
use Cwd qw(chdir cwd);
no warnings "experimental::smartmatch";
our $path = $ENV{"PATH"};

if($path =~ /sspider\/bin/){
    print "Scrapy virtual environment opened already\n";
    print "Do you want to close it? [y/n]:";
    chomp(my $answer = <STDIN>);
    given($answer){
        when(/[yY\n]/){
            my @path = split(/:/, $path);
            my $scrapy_path;
            for (@path){
                if(/sspider/){
                    $scrapy_path = $_;
                }
            };
            print $ENV{PWD}, "\n";
            chdir("$scrapy_path") or die "Can't goto scrapy bin directory";
            print $ENV{PWD}, "\n";
            system("deactivate");
            print "Closed successfully\n";
        }
    }
}

Because when I execute the perl script, it is running in a new process, so it can't close the virtual environment.

So, I want to know how to solve it.(Execute a command in the current bash process in perl script.

Process

Perl

4
  • In bash: exec myscript.pl to replace the bash process with it? Commented May 28, 2022 at 13:43
  • 1
    The solution is to stop using virutalenv. The entire approach is fatally flawed and absurd. But....all it really does is set a few environment variables like PYTHONPATH and PATH and VIRTUALENV. Read through the activate script and see what it's doing...it's not much. Set your PYTHONHOME as needed and move one. Commented May 28, 2022 at 14:12
  • @Shawn I can't understand your meaning, when I use exec command, the bash process exit at the moment. Commented May 28, 2022 at 14:31
  • @WilliamPursell I use the venv moudle, and I write this script just for fun.-:) Commented May 28, 2022 at 14:32

1 Answer 1

1

It is impossible to run Perl code as part of the current bash process. Bash cannot execute Perl code by its own so it needs to run the Perl interpreter - which is separate program and thus will run as a new process.

What might be done though is to create some file by the Perl program which then gets sourced by the shell - thus running the shell-instructions in this created file in the context of the current shell.

Sign up to request clarification or add additional context in comments.

8 Comments

Ok, thanks for your answer. I thought that I can with some command go to the parent process, so funny.-:)
Re "I thought that I can with some command go to the parent process", You can find the parent process. But then what? How do you tell an already running bash to execute a command? That's the impossible part.
@ikegami I know it's impossible, so I add "so funny" after that.
@zhou alias Re "I know it's impossible", I said it's NOT impossible. It's quite easy, in fact, to "go to" the parent process.
@ikegami Sorry, these day I can't login this website.Can you tell me how to "go to" the parent process with perl?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.