Perl output redirection and gnu less problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gnidoc
    New Member
    • Aug 2008
    • 1

    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:
    Code:
    perl -e 'print "test"' | less
    ls | less
    cat /etc/passwd | less
    The problem is that using my script 'less' only gets the lines sent to STDERR.


    linkchk.pl
    Code:
    #!/usr/bin/perl
    #
    # Usage: ./linkchk.pl filename_containing_urls
    # or cat file | ./linkchk.pl
    
    use LWP;
    $ua=LWP::UserAgent->new;
    $ua->timeout(3);
    $req=HTTP::Request->new;
    $req->method("HEAD");
    
    # create and urllist file
    # try ./linkchk.pl urllist | less
    print STDOUT "Why can't ./linkchk.pl urllist|less see this?\n";
    
    while (<>)
    {
            chomp($line=$_);
            $req->uri($line);
            $res=$ua->request($req);
            if ($res->is_success)
            {
              print STDOUT "$line\n";
            }
            else
            {
              print STDERR "[FAIL] $line\n";
            }
    }
    any clues?

    (Fedora9, perl 5.10.0)
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    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 misunderstandin g what you are doing with 'less'.

    --Kevin

    Comment

    Working...