Why is $ENV{COLUMNS} undefined inside the Perl program?

Discussion in 'Perl Misc' started by Adam, Jun 28, 2004.

  1. Adam

    Adam Guest

    The environment variable $COLUMNS works as expected in bash

    /home/adam $ echo $COLUMNS
    165
    [resizes xterm window]
    /home/adam $ echo $COLUMNS
    132

    but $ENV{COLUMNS} is undefined inside a Perl program run from this
    shell. Why? And how do I get its value inside the program?

    I also tried
    use Env qw (COLUMNS) ;
    but $COLUMNS did not get defined.

    --
    Thanks,
    Adam
    Adam, Jun 28, 2004
    #1
    1. Advertising

  2. Adam

    Anno Siegel Guest

    Adam <> wrote in comp.lang.perl.misc:
    > The environment variable $COLUMNS works as expected in bash


    No. $COLUMNS is a "normal" shell variable in bash, not an environment
    variable.

    > /home/adam $ echo $COLUMNS
    > 165
    > [resizes xterm window]
    > /home/adam $ echo $COLUMNS
    > 132
    >
    > but $ENV{COLUMNS} is undefined inside a Perl program run from this
    > shell. Why? And how do I get its value inside the program?


    You don't. Only environment variables are taken over by Perl. Further,
    even if you managed to transfer the variable to Perl's environment,
    it would lose the property of reflecting the current size of the
    terminal.

    For alternatives, see perldoc -q screen.

    Anno
    Anno Siegel, Jun 28, 2004
    #2
    1. Advertising

  3. Adam

    Adam Guest

    On Monday 28 June 2004 12:49, Anno Siegel wrote:

    > No. $COLUMNS is a "normal" shell variable in bash, not an environment
    > variable.


    It shows up in `set` but not `env`. Oops!

    > You don't. Only environment variables are taken over by Perl.
    > Further, even if you managed to transfer the variable to Perl's
    > environment, it would lose the property of reflecting the current size
    > of the terminal.
    >
    > For alternatives, see perldoc -q screen.


    I think this will do what I need.

    use Term::ReadKey;
    ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();

    Thanks for your help.
    -- Adam
    Adam, Jun 28, 2004
    #3
  4. Adam

    Ben Morrow Guest

    Quoth Adam <>:
    > On Monday 28 June 2004 12:49, Anno Siegel wrote:
    >
    > > No. $COLUMNS is a "normal" shell variable in bash, not an environment
    > > variable.

    >
    > It shows up in `set` but not `env`. Oops!
    >
    > > You don't. Only environment variables are taken over by Perl.
    > > Further, even if you managed to transfer the variable to Perl's
    > > environment, it would lose the property of reflecting the current size
    > > of the terminal.
    > >
    > > For alternatives, see perldoc -q screen.

    >
    > I think this will do what I need.
    >
    > use Term::ReadKey;
    > ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();


    See also $SIG{WINCH} (should that go in the faq answer?).

    Ben

    --
    And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
    * *
    Ben Morrow, Jun 28, 2004
    #4
  5. Adam

    Anno Siegel Guest

    Malcolm Dew-Jones <> wrote in comp.lang.perl.misc:
    > Adam () wrote:
    > : The environment variable $COLUMNS works as expected in bash
    >
    > : /home/adam $ echo $COLUMNS
    > : 165
    > : [resizes xterm window]
    > : /home/adam $ echo $COLUMNS
    > : 132
    >
    > : but $ENV{COLUMNS} is undefined inside a Perl program run from this
    > : shell. Why? And how do I get its value inside the program?
    >
    > To make a bash variable visible you need to export it. What that does is
    > tell bash to put the variable into the environment.
    >
    > $ export COLUMNS


    Yes, but Perl's $ENV{COLUMNS} would freeze the value on invocation. It
    wouldn't follow re-sizings like the bash variable does in the OP's example.

    Anno
    Anno Siegel, Jun 28, 2004
    #5
  6. Adam

    Adam Guest

    On Monday 28 June 2004 21:02, Anno Siegel wrote:

    > Yes, but Perl's $ENV{COLUMNS} would freeze the value on invocation.
    > It wouldn't follow re-sizings like the bash variable does in the OP's
    > example.


    I didn't expect that to happen *inside* the running Perl program. I
    (stupidly?) expected the program to inherit the value from the shell
    that called it, but I had forgotten that environment variables are a
    subset of shell variables.

    The Perl program, which works now, uses GetTerminalSize() from
    Term::ReadKey once at the beginning of the program to fix the width of
    the output, so it's frozen anyway. The program just reads a log file
    and prints sorted excerpts to the console.

    --
    Thanks,
    Adam
    Adam, Jun 28, 2004
    #6
  7. Adam () wrote:
    : The environment variable $COLUMNS works as expected in bash

    : /home/adam $ echo $COLUMNS
    : 165
    : [resizes xterm window]
    : /home/adam $ echo $COLUMNS
    : 132

    : but $ENV{COLUMNS} is undefined inside a Perl program run from this
    : shell. Why? And how do I get its value inside the program?

    To make a bash variable visible you need to export it. What that does is
    tell bash to put the variable into the environment.

    $ export COLUMNS
    Malcolm Dew-Jones, Jun 28, 2004
    #7
  8. Anno Siegel (-berlin.de) wrote:
    : Malcolm Dew-Jones <> wrote in comp.lang.perl.misc:
    : > Adam () wrote:
    : > : The environment variable $COLUMNS works as expected in bash
    : >
    : > : /home/adam $ echo $COLUMNS
    : > : 165
    : > : [resizes xterm window]
    : > : /home/adam $ echo $COLUMNS
    : > : 132
    : >
    : > : but $ENV{COLUMNS} is undefined inside a Perl program run from this
    : > : shell. Why? And how do I get its value inside the program?
    : >
    : > To make a bash variable visible you need to export it. What that does is
    : > tell bash to put the variable into the environment.
    : >
    : > $ export COLUMNS

    : Yes, but Perl's $ENV{COLUMNS} would freeze the value on invocation. It
    : wouldn't follow re-sizings like the bash variable does in the OP's example.

    si, but that was already pointed out. Consider

    $ export MY_DEBUG_SETTINGS_FOR_ALL_UTILITIES

    I think he will have the same question next time he wants to access a
    bash variable.
    Malcolm Dew-Jones, Jun 29, 2004
    #8
  9. Adam

    Adam Guest

    On Monday 28 June 2004 13:38, Adam wrote:

    > I think this will do what I need.
    >
    > use Term::ReadKey;
    > ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();


    And indeed it does. The only problem is that if the program is run
    non-interactively (e.g. from at or cron) it halts with an error here.
    I assume there is no way to catch the error?

    I'm now using the I_am_interactive subroutine from the Perl Cookbook
    first and calling GetTerminalSize() only if the program seems to be
    interactive -- is this the best way to handle this situation?

    --
    Thanks,
    Adam
    Adam, Jul 4, 2004
    #9
  10. Adam

    Joe Smith Guest

    Adam wrote:

    >> use Term::ReadKey;
    >> ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();

    >
    > And indeed it does. The only problem is that if the program is run
    > non-interactively (e.g. from at or cron) it halts with an error here.
    > I assume there is no way to catch the error?


    You can catch errors using eval{}.

    use Term::ReadKey;
    my ($wchar, $hchar, $wpixels, $hpixels);
    eval { ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize() };
    if (defined $wchar) {
    print "Width and height = $wchar, $hchar\n";
    } else {
    print "Terminal size is undefined\n";
    }
    Joe Smith, Jul 5, 2004
    #10
  11. Adam

    Adam Guest

    On Monday 05 July 2004 12:55, Joe Smith wrote:

    > You can catch errors using eval{}.
    >
    > use Term::ReadKey;
    > my ($wchar, $hchar, $wpixels, $hpixels);
    > eval { ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize() };
    > if (defined $wchar) {
    > print "Width and height = $wchar, $hchar\n";
    > } else {
    > print "Terminal size is undefined\n";
    > }


    Thanks! I'd forgotten about using eval for error-catching.
    Adam, Jul 7, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ankit Mehta
    Replies:
    1
    Views:
    1,391
    Simon Brooke
    Sep 29, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,739
    Smokey Grindel
    Dec 2, 2006
  3. pedro mg

    ENV['COLUMNS']

    pedro mg, Feb 10, 2007, in forum: Ruby
    Replies:
    7
    Views:
    111
    Ryan Davis
    Feb 11, 2007
  4. TDR
    Replies:
    3
    Views:
    162
    Daniel Berger
    Aug 31, 2007
  5. ad
    Replies:
    5
    Views:
    319
    Uri Guttman
    May 30, 2004
Loading...

Share This Page