SourceForge has been redesigned. Learn more.

Using putenv

  • Duke Normandin

    Duke Normandin - 2010-02-17

    I'm new to Rexx!

    Learning the language in context of CGI.

    After reading the latest pdf docs , am I to understand that `putenv' _cannot_ create new env_vars. It can
    only set an existing one to something else - including an empty string?

    I ask because I'm having trouble with this code:


    say 'Your default editor is now : ' ed

    However, instead of setting $EDITOR = pico, it launches the current $EDITOR.

    dnormandin@ ~
    06:38 pm >> rexx -v
    REXX-Regina_3.5 5.00 31 Dec 2009

    on OS X 10.5.8

  • Mark Hessling

    Mark Hessling - 2010-02-18

    The putenv BIF like the more portable VALUE BIF can create new environment variables or change existing environment variables in the CURRENT process. What you are trying to do is to set an environment variable in the PARENT process.

    Running a shell (process 55)
    ./rexx mysetenv
      This starts a new process as a child of the shell, say process 66
      Call Value 'EDITOR', 'pico', 'ENVIRONMENT'
      Process 66 now has an environment variable of EDITOR=pico
      Exit the running rexx process (process 66 dies)
    You are back to process 55 and the child has died taking its environment with it
    Process 55 EDITOR environment variable is unchanged.

    Now if you try this with running another shell from process 55, you will get exactly the same behaviour.  If you try it with any other scripting language you will get exactly the same behaviour.

    The only way you can get a Rexx program to change your CURRENT process' environment is if the Rexx program runs as part of the CURRENT process. AFAIK the only option is to use zsh as your shell and install the zregina-zsh-module (from Regina SourceForge Downloads). TBH I haven't checked if that still works with the latest zsh or the latest Regina.

    Cheers, Mark

  • Duke Normandin

    Duke Normandin - 2010-02-19

    Thanks for the response….

    From within process 66 (i.e. inside a running rexx script) should it not be possible to "say" what the new value of the Env. Var. is, should I not? After all, I'm still in process 66.

    BTW, where does the bulk of the Regina Rexx community hang out? I see some activity in c.l.rexx, but hardly none in the S.F. ML and Forums.

  • Mark Hessling

    Mark Hessling - 2010-02-19

    Yes, within process 66 you can get the value of the environment variable you just set with the VALUE BIF:
    Say Value( 'EDITOR', , 'ENVIRONMENT' )
    (2nd argument sets an environment variable; no 2nd argument, get the value)

    The regina mailing list. See:
    for a link to getting on the regina-l mailing list

  • Duke Normandin

    Duke Normandin - 2010-02-19

    That's what I thought. So why does this code not work:

    /* script to play with Env. Vars. from inside a running Rexx script */

    say 'Your default editor is: ' getenv('EDITOR')

    Value('EDITOR', 'pico', 'ENVIRONMENT')

    ed = Value( 'EDITOR', , 'ENVIRONMENT' )

    say 'Your default editor is now : ' ed

    What happens when I run the script is that the editor defined in $EDITOR is run - something in the code is actually invoking the editor pointed to by $EDITOR. When I bail from the editor, the the rest of the script runs.

    As far as "regina-l mailing list" goes, I subscribed to the list this morning, but quickly unsubscribed when the majordomo welcome message said that the list was for developers only, and not for some yokel asking questions qhile in the process of learning Rexx. So exactly what is the "regina-l mailing list" for?

  • Mark Hessling

    Mark Hessling - 2010-02-19

    The reason for your behaviour is that you are invoking function calls incorrectly.

    There are two ways of executing BIFS: one as a called procedure which does not return a value, and as a function which does return a value.

    The line:
    Value( 'EDITOR', 'pico', 'ENVIRONMENT')
    is the problem. What a Rexx interpreter does with this (calling as a function and requiring a return value):
    - obtain the original value of the 'EDITOR' environment variable as the return value; lets assume it is 'vi'
    - set the new value of 'EDITOR' to 'pico'
    - pass the return value from the VALUE BIF ('vi') back to the interpreter
    - execute the return value as a command in the default environment (passes 'vi' to the default environment, which is a shell)
    Hence you get an instance of your previous editor. Once you exit that process, control passes to the next clause in the Rexx program.

    What you should have is (invoke as subroutine; no return value): (as was in my last email)
    Call Value 'EDITOR', 'pico', 'ENVIRONMENT'
    This will NOT return a value to the interpreter, so the retrun value cannot be executed by the interpreter.

    If you are going to be writing Rexx code you probably should look at obtaining a book or reading a tutorial.  The Regina documentation is a Reference Manual not a user guide.
    for some basic introductory assistance with Rexx.

    What this line results in is:

  • Duke Normandin

    Duke Normandin - 2010-02-19

    What do you know - It Works! - when you speak the lingo correctly ;)

    dnormandin@ ~/programming/rexx/code
    07:04 am >> regina test_getenv.rexx

    Your default editor is:  jmacs
    Your default editor is now :  pico

    As far tutorials go, I've taken the same approach with Rexx as I've taken with the other 4 languages that I've taught myself - i.e. I DL as much pedagogal material as I can find, and glean the best from all of them. The trick is _finding_ relevant and up-to-date material. ;) So currently I'm reading:

    Learn REXX Programming in 56,479 Easy Steps By Jeff Glatt

    However, it _was_  "my bad" - I simply missed the point that procedures are invoked using "call" AND no brackets; while a function is invoked by using the function AND brackets containing argument(s).  I also missed the point that, e.g. "Value" call be used either as a function or a procedure/subroutine, depending on how it's invoked.  To date I've yet to come across wording in the tutorials that make that point clear. Or maybe I'm still not grokking it. ;)

    BTW, what about my last point in my previous post - about the mailing list? I'm looking for the most appropriate place to be asking these noob questions - and not so noob ones as time goes on.

    Anyway, thanks for your time and help.


Log in to post a comment.