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

A

Adam

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.
 
A

Anno Siegel

Adam said:
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
 
A

Adam

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
 
B

Ben Morrow

Quoth Adam said:
It shows up in `set` but not `env`. Oops!


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
 
A

Anno Siegel

Malcolm Dew-Jones said:
Adam ([email protected]) 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
 
A

Adam

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.
 
M

Malcolm Dew-Jones

Adam ([email protected]) 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
 
M

Malcolm Dew-Jones

Anno Siegel ([email protected]) wrote:
: > Adam ([email protected]) 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.
 
A

Adam

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?
 
J

Joe Smith

Adam said:
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";
}
 
A

Adam

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.
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top