anyone could explain this to me?

R

robertchen117

what the following sentence in a perl do? Please explain with detail.
Thanks.

grep (do { chop; s/^([^=]+)=(.*)$/$ENV{$1}=$2/e},`. /etc/Tivoli/
setup_env.sh;env`);
 
T

Ted Zlatanov

rc> what the following sentence in a perl do? Please explain with detail.

rc> grep (do { chop; s/^([^=]+)=(.*)$/$ENV{$1}=$2/e},`. /etc/Tivoli/setup_env.sh;env`);

1) `. /etc/Tivoli/setup_env.sh;env`

source that .sh file and print the environment

2) s/^([^=]+)=(.*)$/$ENV{$1}=$2/e

take all the environment variables from (1) and put them into the
current environment (using grep for side effects, which is naughty)

So basically it's an unnecessarily complex way to grab the environment
resulting from /etc/Tivoli/setup_env.sh

Ted
 
P

Peter J. Holzer

rc> what the following sentence in a perl do? Please explain with detail.

rc> grep (do { chop; s/^([^=]+)=(.*)$/$ENV{$1}=$2/e},`. /etc/Tivoli/setup_env.sh;env`);

1) `. /etc/Tivoli/setup_env.sh;env`

source that .sh file and print the environment

2) s/^([^=]+)=(.*)$/$ENV{$1}=$2/e

take all the environment variables from (1) and put them into the
current environment (using grep for side effects, which is naughty)

So basically it's an unnecessarily complex way to grab the environment
resulting from /etc/Tivoli/setup_env.sh

While I would have written that as:

for (`. /etc/Tivoli/setup_env.sh; env`) {
$ENV{$1} = $2 if (/^([^=]+)=(.*)$/);
}

that's not less complex, just more readable (IMHO). Can you think of a
less complex way to do that? (assuming that /etc/Tivoli/setup_env.sh may
contain non-trivial /bin/sh code)

hp
 
A

anno4000

Peter J. Holzer said:
rc> what the following sentence in a perl do? Please explain with detail.

rc> grep (do { chop; s/^([^=]+)=(.*)$/$ENV{$1}=$2/e},`. /etc/Tivoli/setup_env.sh;env`);

1) `. /etc/Tivoli/setup_env.sh;env`

source that .sh file and print the environment

2) s/^([^=]+)=(.*)$/$ENV{$1}=$2/e

take all the environment variables from (1) and put them into the
current environment (using grep for side effects, which is naughty)

So basically it's an unnecessarily complex way to grab the environment
resulting from /etc/Tivoli/setup_env.sh

While I would have written that as:

for (`. /etc/Tivoli/setup_env.sh; env`) {
$ENV{$1} = $2 if (/^([^=]+)=(.*)$/);
}

that's not less complex, just more readable (IMHO). Can you think of a
less complex way to do that? (assuming that /etc/Tivoli/setup_env.sh may
contain non-trivial /bin/sh code)

The regex could be simplified: /(.*)=(.*)/ would capture the same
things, given greediness and the structure of env output. I'd
probably use

my ( $name, $value) = split /=/, $_, 2;
$ENV{ $name} = $value;

Anno
 
D

Dr.Ruud

(e-mail address removed)-berlin.de schreef:
The regex could be simplified: /(.*)=(.*)/ would capture the same
things, given greediness and the structure of env output.

Not if any environment variable would contain an "=".

echo test=a=b |perl -wnle'
print "$1/$2" if /^(test.*)=(.*)/
'
 
P

Peter J. Holzer

Peter J. Holzer said:
On 15 Mar 2007 08:28:39 -0700 "(e-mail address removed)"
rc> grep (do { chop; s/^([^=]+)=(.*)$/$ENV{$1}=$2/e},`.
rc> /etc/Tivoli/setup_env.sh;env`); [...]
So basically it's an unnecessarily complex way to grab the environment
resulting from /etc/Tivoli/setup_env.sh

While I would have written that as:

for (`. /etc/Tivoli/setup_env.sh; env`) {
$ENV{$1} = $2 if (/^([^=]+)=(.*)$/);
}

that's not less complex, just more readable (IMHO). Can you think of a
less complex way to do that? (assuming that /etc/Tivoli/setup_env.sh may
contain non-trivial /bin/sh code)

The regex could be simplified: /(.*)=(.*)/ would capture the same
things, given greediness and the structure of env output.

Nope, but /(.*?)=(.*)/ would (and should work without excessive
backtracking). I admit that I haven't looked at the regex that much.
It seems pretty straightforward and rather simple, and I don't find
/(.*?)=(.*)/ less complex than /^([^=]+)=(.*)$/ (it's shorter, but we
aren't playing golf here).

I'd probably use

my ( $name, $value) = split /=/, $_, 2;
$ENV{ $name} = $value;

Ok, split may be considered less complex than a capturing regex, even
with the 3rd parameter.

hp
 
T

Ted Zlatanov

PJH> While I would have written that as:

PJH> for (`. /etc/Tivoli/setup_env.sh; env`) {
PJH> $ENV{$1} = $2 if (/^([^=]+)=(.*)$/);
PJH> }

PJH> that's not less complex, just more readable (IMHO). Can you think of a
PJH> less complex way to do that? (assuming that /etc/Tivoli/setup_env.sh may
PJH> contain non-trivial /bin/sh code)

No, your approach is what I had in mind. Obviously, if we could
execute in the context of setup_env.sh, that would be best, but
perhaps the script shouldn't have the environment for the entire run.

Your approach has one loop, and it's very readable. The original had
unnecessary grep, do, and chop. That's what I meant by "unnecessarily
complex."

Ted
 
A

anno4000

Dr.Ruud said:
(e-mail address removed)-berlin.de schreef:


Not if any environment variable would contain an "=".

echo test=a=b |perl -wnle'
print "$1/$2" if /^(test.*)=(.*)/

Well, it's a shell variable, it can't. That's in the structure of
env output.

Anno
 
A

anno4000

Peter J. Holzer said:
Sure it can:

yoyo:~/tmp 17:22 187% export foo=bar=baz
yoyo:~/tmp 20:53 188% env | grep foo
foo=bar=baz

Ah, I've been wrong a few steps back in the thred. What I meant to
say was a shell variable *name* can't contain a "=", which it can't.
For some reason I believed the regex /(.*)=(.*)/ would capture in $1
everything up to the first blank, which is plainly wrong. Sorry.

Same apology goes for Sherm Pendley's reply.

Anno
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top