You'll need to select the filehandle that you wish to modify the buffering on before changing the $| variable that Ryszard points out.
An exception to that is STDOUT.
By default STDOUT is selected, so if that's the only filehandle that you want to turn buffering off for, then there's no need to select STDOUT first - just do $|++.
If you are changing the buffering on a number of filehandles, it's considered good practice to (re)select STDOUT before carrying out any other processing.
Not doing this might cause havoc with your print statements, and confuse anyone who has to maintain the code in future:
open(LOG, "> ./logfile") or die "$!";
open(DATA "> ./data") or die "$!";
select LOG;
$|++;
select DATA;
$|++;
select STDOUT;
Another option is to use IO::Handle:
use IO::Handle;
open(LOG, "> ./logfile");
LOG->autoflush(1);
Have a look at perlfaq5 for more info and a slightly obfu way of using select :-)
Cheers.
BazB
Update: fixed code tag and small syntax changes.
If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
That way everyone learns.
| [reply] [d/l] [select] |
ok guys, I thought flushing the buffer would help, apparently not. I'm using select as a sort of timeout for data on an incomming socket. The scenario goes like this. I've got a socket waiting for data, if it's there enterpret it, if however, no data has been recieved within 30 seconds, check the status of the line with a pseudo ping to make sure the host on the other side is listening. Here's the problem, when the aplication starts, it never times out, ONLY when I press return on the kb does it start to time out. I'm thinking it's because for SOME reason, the socket wasn't updated and it's still waiting for data on it, wich wasn't the case.
my $selectFD;
$selectFD = new IO::Select($rec[$RXThreadNumber], undef, undef, 1);
$|++; # flush the buffer apparently...
while(@bits = $selectFD->can_read)
{
select(undef, undef, undef, 0.1);
foreach $fdThread (@bits)
{
if($fdThread == $rec[$RXThreadNumber])
{
print("Fetching pdu\n");
# the following if is to make sure that the only time we try
+ fetch an sms
# is when there is actual sms activity on the socket... and
+nothing else...
# this was causing problems before because a sock kill init
+was creating# noise on the line and the aplication was trying to ent
+erperet this as
# a PDU...
if($pdu = $rec[$RXThreadNumber]->read_pdu())
{
print("CMD: $pdu->{cmd}\n");
print("STATUS: $pdu->{status}\n");
print("SERTYPE: $pdu->{service_type}\n");
print("SEQ: $pdu->{seq}\n");
print("SOURCE: $pdu->{source_addr}\n");
print("DESTINATION: $pdu->{destination_addr}\n");
#print("DATA: $pdu->{data}\n");
print("MESSAGE: $pdu->{short_message}\n");
$test = $rec[$RXThreadNumber]->deliver_sm_resp(seq=>$pd
+u->{seq}, message_id=>'');
my $nowRXTime = genTime();
my $queryRX = $tdbh->prepare("insert into MOHits values
+(NULL, NULL, \"$pdu->{source_addr}\", \"$nowRXTime\", \"$pdu->{short_
+message}\", 0, \"$pdu->{destination_addr}\")");
$queryRX->execute;
print("Fetched pdu\n");
}
}
if((time() - $startTime) > 5)
{
$startTime = time();
print("Enquiring link...\n");
if($rec[$RXThreadNumber]->enquire_link())
{
print("Link is fine...\n");
}
else
{
print("Link is DOWN! Bringing up again...\n");
$rec[$RXThreadNumber] = Net::SMPP-new_receiver(
$tshost,
smpp_version => $smppVer,
system_id => $tssysid,
password => $tspassword,
system_type => $tssystype,
dest_addr_ton => 0x91, # Type Of Number,
+this means its "2783XXXXXXX"
dest_addr_npi => 0x00,
addr_ton => 0x91,
addr_npi => 0x00,
source_addr_ton => 0x91,
source_addr_npi => 0x00,
port => $tsport
) or warn("can't connect to smsc: $!\n");
}
}
}
}
The timeout ONLY fires when I press return on the kb once the aplication starts.... any idea's?
Thanks
Scuzz. (; | [reply] [d/l] |
This line waits forever for input:
while(@bits = $selectFD->can_read)
and then this line pauses for a tenth of second for no apparent reason:select(undef, undef, undef, 0.1);
What you probably want to do is:@bits = $selectFD->can_read(30);
if (@bits == 0) {
# we timed out
} else {
for (@bits) {
blah blah blah
}
}
| [reply] [d/l] [select] |
eval {
local $SIG{ALRM} = sub { die "Timed Out!\n" };
my $timeout = 30; # give 30 seconds to accept and process data
my $previous_alarm = alarm($timeout);
# get input
# and do heavy and interesting and cool stuff :-)
# ...
# finished with this
alarm($previous_alarm)
}; # eval
if ($@ =~ /timed out/i) {
# check stuff, report error, etc
}
hth
regards,
tomte
Edit:Fixed indentation.
| [reply] [d/l] |
| [reply] |
$|++;
$|=1;
$|=21%5;
Just to name a few ways to flush the buffer. | [reply] [d/l] |