kurta has asked for the wisdom of the Perl Monks concerning the following question:

This is a weird one. I'm reading data from a GPS module. The following code gives the expected response:

#! /opt/local/bin/perl -w use strict; require 5.000; use lib "/opt/local/lib/perl5/"; use Data::Dump qw(dump); use Device::SerialPort; my $port = Device::SerialPort->new("/dev/tty.usbserial"); $port->are_match("\r\n"); $port->baudrate(9600); $port->databits(8); $port->parity("none"); $port->stopbits(1); while (1) { my $s = $port->lookfor(); next if $s eq ''; print $s,"\n"; if ($s=~/.*GPGGA.*/){ my @a=$s=~/(\d+\.\d{2}),(\d+\.\d+),(N|S),(\d+\.\d+),(E|W)/; print "==",dump(@a); } }
But if I comment out the "print $s,"\n";" statement, I get nothing. Here's some sample output:

$GPRMC,108746.88,A,2048.37808,N,80113.44007,W,8.352,,388416,,,D*61
$GPVTG,,T,,M,8.352,N,8.652,K,D*23
$GPGGA,108746.88,2048.37808,N,80113.44007,W,2,89,1.82,26.8,M,-29.6,M,,8888*51
==("108746.88", "2048.37808", "N", "80113.44007", "W")$GPGSA,A,3,16,26,22,31,23,27,51,80,89,,,,1.78,1.82,1.36*83
$GPGSV,4,1,14,83,15,210,80,84,11,840,13,87,13,381,,80,47,173,32*7F
$GPGSV,4,2,14,89,42,311,31,11,83,103,,16,58,824,29,22,87,193,11*7E
$GPGSV,4,3,14,23,78,208,20,26,26,844,19,27,63,117,26,31,18,897,17*7D

I suspect there's something strange going on with buffering, but don't understand why printing a variable should make a difference. Any perl-ly wisdom would be appreciated

=== ARGH! Just added a "/n" to the print "==",dump(@a); line and now it works. Why doesn't the buffer flush on print? Is Device::SerialPort resetting something?

Replies are listed 'Best First'.
Re: Device::SerialPorDevice::SerialPort;t Problem
by Discipulus (Canon) on Apr 30, 2016 at 20:02 UTC
    to add something to what wisely Anonymonk said, look for $| in perlvar and profit the read of suffering from buffering

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Device::SerialPorDevice::SerialPort;t Problem
by Anonymous Monk on Apr 30, 2016 at 19:10 UTC

    Why doesn't the buffer flush on print?

    Because STDOUT is buffered by default, you have to disable buffering

    use IO::Handle; STDOUT->autoflush;