Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 10th, 2008, 03:03 PM
Newbie
 
Join Date: Aug 2008
Posts: 1
Default Perl output redirection and gnu less problem

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)
Reply
  #2  
Old August 17th, 2008, 10:00 PM
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 837
Default

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
Reply
Reply

Bookmarks

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 Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles