By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,691 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,691 IT Pros & Developers. It's quick & easy.

Perl output redirection and gnu less problem

P: 1
This is a small perl script I wrote to check mirror lists (files filled with one URL per line). It prints "[FAIL] http://..." when an URL can't be retrieved and just prints the url when everything goes fine. The code is pretty straightforward and it works well until I am trying to redirect its output.

There is one thing I can't figure out. As far as I know the program called 'less' prints its input on the screen providing a nice interface for examining the contents of another program's output.

less works fine in situations like:
Expand|Select|Wrap|Line Numbers
  1. perl -e 'print "test"' | less
  2. ls | less
  3. cat /etc/passwd | less
The problem is that using my script 'less' only gets the lines sent to STDERR.


linkchk.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. #
  3. # Usage: ./linkchk.pl filename_containing_urls
  4. # or cat file | ./linkchk.pl
  5.  
  6. use LWP;
  7. $ua=LWP::UserAgent->new;
  8. $ua->timeout(3);
  9. $req=HTTP::Request->new;
  10. $req->method("HEAD");
  11.  
  12. # create and urllist file
  13. # try ./linkchk.pl urllist | less
  14. print STDOUT "Why can't ./linkchk.pl urllist|less see this?\n";
  15.  
  16. while (<>)
  17. {
  18.         chomp($line=$_);
  19.         $req->uri($line);
  20.         $res=$ua->request($req);
  21.         if ($res->is_success)
  22.         {
  23.           print STDOUT "$line\n";
  24.         }
  25.         else
  26.         {
  27.           print STDERR "[FAIL] $line\n";
  28.         }
  29. }
  30.  
any clues?

(Fedora9, perl 5.10.0)
Aug 10 '08 #1
Share this Question
Share on Google+
1 Reply


eWish
Expert 100+
P: 971
It appears that you are not using 'less' in the proper manner. This maybe be the reason that it is printing to STDERR. What is the error message that it is printing?

Unless, I am completely misunderstanding what you are doing with 'less'.

--Kevin
Aug 17 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.