LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-10-2008, 09:50 AM   #1
gnidoc
LQ Newbie
 
Registered: Aug 2008
Posts: 2

Rep: Reputation: 0
Perl to gnu less redirection problem


This is a small perl script I wrote to check mirror lists (files filled with one URL per line). It prints "[FAIL] hxxp://..." 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)
 
Old 08-10-2008, 12:33 PM   #2
ne pas
Member
 
Registered: Jul 2008
Posts: 55

Rep: Reputation: 23
First thought: STDOUT output is line buffered on default. Set autoflush to a non-zero value, right after your Shebang (#!)
Code:
#!/usr/bin/perl
$| = 1;

# or more common

$|++;

# also possible

use IO::Handle;

STDOUT->autoflush(1);
 
Old 08-10-2008, 04:01 PM   #3
gnidoc
LQ Newbie
 
Registered: Aug 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Both does the trick, thanks for your help!
 
  


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
Problem with I/O redirection manyoo Programming 6 04-11-2007 05:06 AM
[Perl] chdir and redirection noir911 Programming 5 12-10-2006 06:08 PM
Redirection problem studentlb Programming 18 12-04-2006 01:28 AM
Bash I/O redirection problem R00ts Programming 1 10-26-2006 07:50 PM
Apache - Redirection problem... elitecodex Linux - Software 5 03-29-2004 10:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:08 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration