Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Why is $ENV{COLUMNS} undefined inside the Perl program?

Reply
Thread Tools

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

 
 
Adam
Guest
Posts: n/a
 
      06-28-2004
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

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      06-28-2004
Adam <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Adam
Guest
Posts: n/a
 
      06-28-2004
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

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      06-28-2004

Quoth Adam <(E-Mail Removed)>:
> 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)
* http://www.velocityreviews.com/forums/(E-Mail Removed) *
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      06-28-2004
Malcolm Dew-Jones <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Adam ((E-Mail Removed)) 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
 
Reply With Quote
 
Adam
Guest
Posts: n/a
 
      06-28-2004
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

 
Reply With Quote
 
Malcolm Dew-Jones
Guest
Posts: n/a
 
      06-28-2004
Adam ((E-Mail Removed)) 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
 
Reply With Quote
 
Malcolm Dew-Jones
Guest
Posts: n/a
 
      06-28-2004
Anno Siegel ((E-Mail Removed)-berlin.de) wrote:
: Malcolm Dew-Jones <(E-Mail Removed)> wrote in comp.lang.perl.misc:
: > Adam ((E-Mail Removed)) 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.

 
Reply With Quote
 
Adam
Guest
Posts: n/a
 
      07-04-2004
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
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      07-05-2004
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";
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
typeof x == 'undefined' or x == undefined? -Lost Javascript 13 01-31-2007 12:04 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
undefined vs. undefined (was: new Array() vs []) VK Javascript 45 09-12-2006 05:26 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments