For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > December 2004 > What exactly does "$| = 1;" do?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author What exactly does "$| = 1;" do?
Robert

2004-11-30, 3:55 pm

I have seen in a few scripts now, including some of the articles that Mr.
Schwartz has written. I have read it does something with the buffers but on
a more technical level what is that?

Robert


Wiggins d Anconia

2004-11-30, 3:55 pm

> I have seen in a few scripts now, including some of the articles that Mr.
> Schwartz has written. I have read it does something with the buffers

but on
> a more technical level what is that?
>
> Robert
>


Since this is a general Perl question not related to CGI it is better
asked to beginners@perl.org....

perldoc perlvar

Will give probably the least friendly and most technical version.
Specifically under OUTPUT_AUTOFLUSH. The tricky parts being that it is
specifically the "currently selected" output, see,

perldoc -f select (first form)

For more on that, and that buffering may or may not be implemented for a
given type of handle (though most are).

Helps?

http://danconia.org
Ing. Branislav Gerzo

2004-11-30, 3:55 pm

Robert [R], on Tuesday, November 30, 2004 at 09:15 (-0500) contributed
this to our collective wisdom:

R> I have seen in a few scripts now, including some of the articles that Mr.
R> Schwartz has written. I have read it does something with the buffers but on
R> a more technical level what is that?

it flushes output by default, so every write is really written.
= do not buffer.

--

...m8s, cu l8r, Brano.

[Assembler Command: RCP: Reschedule Car Payments]


Randal L. Schwartz

2004-11-30, 3:55 pm

>>>>> "Wiggins" == Wiggins d Anconia <wiggins@danconia.org> writes:

Wiggins> Since this is a general Perl question not related to CGI it is better
Wiggins> asked to beginners@perl.org....

Actually, I have sympathy for this one. It's most often used in CGI
scripts so that a naive "system" invocation doesn't come out before
the headers.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Todd W

2004-12-16, 3:55 pm


"Robert" <catcher@linuxmail.org> wrote in message
news:20041130141505.9079.qmail@lists.develooper.com...
> I have seen in a few scripts now, including some of the articles that Mr.
> Schwartz has written. I have read it does something with the buffers but

on
> a more technical level what is that?
>
> Robert
>


Along with the other explinations, consider an example:

[trwww@waveright trwww]$ perl
use warnings;
use strict;

foreach my $i ( 1 .. 5 ) {
print($i, '...');
sleep( 1 );
}
print( "\n" );

$| = 1;
sleep( 1 );

foreach my $i ( 1 .. 5 ) {
print($i, '...');
sleep( 1 );
}
print( "\n" );
^D
1...2...3...4...5...
1...2...3...4...5...

The first line is all printed out at once. The second line prints '1...'
then waits a second then prints '2...' and so on.

Todd W.




Jan Eden

2004-12-20, 8:55 am

Hi Todd,

Todd W wrote on 16.12.2004:

>
>"Robert" <catcher@linuxmail.org> wrote in message
>news:20041130141505.9079.qmail@lists.develooper.com...
=2E[color=darkred]
>on
>
>Along with the other explinations, consider an example:
>
>[trwww@waveright trwww]$ perl
>use warnings;
>use strict;
>
>foreach my $i ( 1 .. 5 ) {
> print($i, '...');
> sleep( 1 );
>}
>print( "\n" );
>
>$| =3D 1;
>sleep( 1 );
>
>foreach my $i ( 1 .. 5 ) {
> print($i, '...');
> sleep( 1 );
>}
>print( "\n" );
>^D
>1...2...3...4...5...
>1...2...3...4...5...
>
>The first line is all printed out at once. The second line prints
>'1...' then waits a second then prints '2...' and so on.
>


That's what I knew about $|, too. But with the following script, it does no=
t work as expected:

___BEGIN CODE___

#!/usr/bin/perl -wT

use CGI;
use strict;
use LWP::UserAgent;

my $q =3D new CGI;

print $q->header(-type=3D>'text/html', -charset=3D>'utf-8'),
$q->start_html("URL-Checker"),
$q->h1('URL-Checker');
=20
if ($q->param) {
my $input =3D $q->param('input');
my @urls =3D split /[\r\n]+/, $input;
my $fh =3D $q->upload('input_file');
while (<$fh> ) {
my @line =3D split /[\r\n]+/;
push @urls, @line;
}
for (@urls) {
# Hinzuf=FCgen des Schemas
$_ =3D "http://" . $_ unless /^http/;
my $browser =3D LWP::UserAgent->new( );
# Um eine automatische Weiterleitung zu vermeiden: weitergeleitete =
URLs erscheinen als invalid!
$browser->requests_redirectable([]) if ($q->param('no_redirect'));
my $response =3D $browser->get($_);
if ($response->status_line eq "200 OK") {
print qq{<p><span style=3D"color:green">'$_'</span> is valid</p=
>};

}
else { =20
print qq{<p><span style=3D"color:red">'$_'</span> is not valid<=
br /><b>Response status:</b> }, $response->status_line, "</p>";
}
}
}
else {
# html form is displayed here
}

print $q->end_html;

___END CODE___

This checks all URLs passed to the script and prints them out at once. When=
I add $| =3D 1; somewhere near the top, it also seems to buffer the output=
=2E I would expect the lines to appear one after the other.

What am I doing wrong?

Thanks,

Jan
--=20
There are two major products that come out of Berkeley: LSD and UNIX. We do=
n't believe this to be a coincidence. - Jeremy S. Anderson
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com