anyone could explain this to me?

Discussion in 'Perl Misc' started by robertchen117, Mar 15, 2007.

  1. 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`);
     
    robertchen117, Mar 15, 2007
    #1
    1. Advertisements

  2. robertchen117

    Ted Zlatanov Guest

    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
     
    Ted Zlatanov, Mar 15, 2007
    #2
    1. Advertisements

  3. 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
     
    Peter J. Holzer, Mar 15, 2007
    #3
  4. robertchen117

    anno4000 Guest

     
    anno4000, Mar 15, 2007
    #4
  5. robertchen117

    Dr.Ruud Guest

    -berlin.de schreef:
    Not if any environment variable would contain an "=".

    echo test=a=b |perl -wnle'
    print "$1/$2" if /^(test.*)=(.*)/
    '
     
    Dr.Ruud, Mar 15, 2007
    #5
  6. 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).

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

    hp
     
    Peter J. Holzer, Mar 15, 2007
    #6
  7. robertchen117

    Ted Zlatanov Guest

    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
     
    Ted Zlatanov, Mar 16, 2007
    #7
  8. robertchen117

    anno4000 Guest

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

    Anno
     
    anno4000, Mar 18, 2007
    #8
  9. Sure it can:

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

    hp
     
    Peter J. Holzer, Mar 18, 2007
    #9
  10. robertchen117

    anno4000 Guest

    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
     
    anno4000, Mar 18, 2007
    #10
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.