Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 08-10-2008, 09:50 AM   #1
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:
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.
# Usage: ./ filename_containing_urls
# or cat file | ./

use LWP;

# create and urllist file
# try ./ urllist | less
print STDOUT "Why can't ./ urllist|less see this?\n";

while (<>)
        if ($res->is_success)
          print STDOUT "$line\n";
          print STDERR "[FAIL] $line\n";
any clues?

(Fedora9, perl 5.10.0)
Old 08-10-2008, 12:33 PM   #2
ne pas
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 (#!)
$| = 1;

# or more common


# also possible

use IO::Handle;

Old 08-10-2008, 04:01 PM   #3
LQ Newbie
Registered: Aug 2008
Posts: 2

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


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 > Forums > Linux Forums > Linux - Newbie

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

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