Two questions about perl

W

Weirong Zhu

On linux

1. I have a perl script, the main body of which is a for loop. In each
iteration the script use "system" to call outside exe to do some work.
The whole script may run very long time. Sometimes, I want to use
Ctrl+C to terminate the script. However, when I use ctrl+c, I can only
terminate the child process invoked by system, the script itself is
still running. Then I have to open another term to kill the script by
its pid.
I want to know how can I kill the script process by ctrl+c directly in
this conditon. Please aware that I can not use the return value of
system to do this. Because even the return value of system means a
error, the script just think this iteration failed, it will continue
the next iteration.

2. It seems change the $ENV{"xxx"} only takes effect in child process.
How can I set the enviroment variable in the middle of a perl script?
For example, I want to change the $LANG several times, so that I can
print different language's time format by using "localtime"

Thanks
 
B

Bill Smith

Weirong Zhu said:
--snip question 1--
2. It seems change the $ENV{"xxx"} only takes effect in child process.
How can I set the enviroment variable in the middle of a perl script?
For example, I want to change the $LANG several times, so that I can
print different language's time format by using "localtime"

Thanks

It is not possible to pass any information from a child process to its
parent using variables. Refer to "perldoc -f die" and "perldoc -f exit"
to learn how to set the return value and "perldoc -f system" to learn
how
to use the return value or use a file to store the current language
name.

Bill
 
L

Lack Mr G M

|>
|> > 2. It seems change the $ENV{"xxx"} only takes effect in child process.
|> > How can I set the enviroment variable in the middle of a perl script?
|> > For example, I want to change the $LANG several times, so that I can
|> > print different language's time format by using "localtime"
|>...
|> It is not possible to pass any information from a child process to its
|> parent using variables.

Whereas this is true, it is not what was asked.

The question was whether setting $ENV{LANG} within a script can
affect the output of a script.

It might be that Perl doesn't use this for localtime() anyway? I
can't get 5.6.1 to print anything other than English even if I set the
this in the environment *before* starting Perl, but the same system's
date command is happy to display in French.
 
S

Simon Andrews

Weirong said:
On linux

2. It seems change the $ENV{"xxx"} only takes effect in child process.
How can I set the enviroment variable in the middle of a perl script?
For example, I want to change the $LANG several times, so that I can
print different language's time format by using "localtime"

If you want to do this you probably need to use the setlocale function
which is part of the POSIX module.

See perldoc perllocale for details about this.

Simon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top