LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-01-2009, 12:07 PM   #1
zerobane
Member
 
Registered: Jan 2006
Posts: 41

Rep: Reputation: 16
perl script get enviromnet varible LINES= for current session


Hello,

Want to return current LINES=, tryng to avoid the "hacky" backticks and string manipulation way
ie: my $somevar = `set | grep LINES`

I tried using the env command and it returns a empty value?
my $lines = $ENV{'LINES'};

$= doesnt work well for putty sessions...
my $lines = $=;


Any other ideas, best practices?

Thanks!

-jc
 
Old 04-01-2009, 12:59 PM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
It's odd: if I do echo $LINES, I get '24', but if I try using Perl, I get nothing corresponding to $LINES:
Code:
#!/usr/local/bin/perl
use strict;
use warnings;

for my $key (keys %ENV) {
  print "$key\t$ENV{$key}\n";
}
 
Old 04-01-2009, 01:12 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by Telemachos View Post
It's odd: if I do echo $LINES, I get '24', but if I try using Perl, I get nothing corresponding to $LINES:
Code:
#!/usr/local/bin/perl
use strict;
use warnings;

for my $key (keys %ENV) {
  print "$key\t$ENV{$key}\n";
}
I have the same effect, however,

(export LINES; perl -e 'print $ENV{LINES},"\n"')

produces 70. So, I guess, LINES is not an environment variable, but just a shell variable.
 
Old 04-01-2009, 01:55 PM   #4
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Quote:
Originally Posted by Sergei Steshenko View Post
(export LINES; perl -e 'print $ENV{LINES},"\n"')

produces 70. So, I guess, LINES is not an environment variable, but just a shell variable.
This works for me as well.

@ zerobane: if you add export LINES into your shell's login configuration file (eg, .bash_profile), Perl should then be able to "see" $ENV{LINES}. (I just tested this and it works for me.)
 
Old 04-02-2009, 02:33 PM   #5
zerobane
Member
 
Registered: Jan 2006
Posts: 41

Original Poster
Rep: Reputation: 16
hmmm

seems difficult for such a simple thing... Maybe im going about this wrong...

What i'm trying to do is print a array that is greater then 1 screen big...

What would be the best way to parse the output?

original plan was to if linecount == count then pause...

This would also work nicely when you are putty'd as the LINES var changes with screen size...

any better ways (couple of scripts i looked at are using | more, which seems kind of "hacky")
 
Old 04-02-2009, 05:15 PM   #6
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Quote:
Originally Posted by zerobane View Post
seems difficult for such a simple thing... Maybe im going about this wrong...

What i'm trying to do is print a array that is greater then 1 screen big...

What would be the best way to parse the output?

original plan was to if linecount == count then pause...

This would also work nicely when you are putty'd as the LINES var changes with screen size...

any better ways (couple of scripts i looked at are using | more, which seems kind of "hacky")
This is either more or less complicated than it seems. It's less complicated if the script is just for you, not especially important and you want something simple to print out a static list. In that case, run your Perl script and pipe its output to the pager of your choice:
Code:
perl my_script | less
As a general rule, I prefer to do it this way - call the pager in the shell - rather than explicitly send output to the pager inside my Perl script, for a couple of reasons. One, not every computer has the same pagers available, and so then I have to allow for fallbacks. Two, I know that I won't always want paged output. Sometimes I will want to redirect the output to a file. If I hardcode that the output goes to a pager, then I'm handcuffed.

However, if this is for production code for a client or many users, then I wouldn't mess around. Look on CPAN. There are many solid modules available that will (1) gather information about the terminal that is running and (2) page through output. This is a more reliable solution for a bigger or more significant piece of code since it's more portable (the paging is done via Perl; you don't rely on what pagers are available) and it can allow for different users who have different sized screens. The first-thought idea of 'line_count == count, then pause' doesn't really work since my terminal may has 24 lines, but Sergei's has 70. If you page every 65 lines, you overrun my terminal by quite a lot. If you page by 20, you annoy the hell out of him. You need your solution - if it's a real application - to allow different users to differ.

Last edited by Telemachos; 04-02-2009 at 05:17 PM.
 
Old 04-02-2009, 05:23 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by zerobane View Post
seems difficult for such a simple thing... Maybe im going about this wrong...

What i'm trying to do is print a array that is greater then 1 screen big...

What would be the best way to parse the output?

original plan was to if linecount == count then pause...

This would also work nicely when you are putty'd as the LINES var changes with screen size...

any better ways (couple of scripts i looked at are using | more, which seems kind of "hacky")
Look, there is 'stty', and just after quickly looking into 'stty --help' I see I can do this:


Code:
stty --all
speed 38400 baud; rows 70; columns 174; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -ixon -ixoff -iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
, so the "rows 70" part is what you need.

Of course, it works from Perl:

Code:
perl -e '`stty --all`=~m/\brows\s+(\d+)/; print "$1\n"'
70
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
search files in the current directory/subdirectory for lines that match particular rajdey1 Linux - Newbie 2 11-24-2008 02:32 PM
Perl Script to select common lines in two files. perluser59 Programming 12 05-26-2008 02:19 AM
NEED HELP IN comment lines PERL Perl script adam_blackice Programming 17 11-07-2007 08:01 AM
Varible substitution in Bash script RonV Linux - Newbie 2 06-04-2006 12:11 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM


All times are GMT -5. The time now is 07:36 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration