Front page | perl.beginners |
Postings from August 2009
Problem with sysread()
Thread Next
From:
Peter Xu
Date:
August 22, 2009 07:30
Subject:
Problem with sysread()
Message ID:
4A9000FF.7070201@gmail.com
Hi, everyone,
I've met some problem while practice my perl. It seems that in some
condition,
sysread() doesn't work?
codes here:
--------------------------------------------
#!/usr/bin/perl -w
use strict;
$| = 1;
open my $file, "<", "test.txt" or die;
## if these add these 2 lines, sysread doesn't work ?
## my $first_line = <$file>;
## print "first_line is : $first_line";
my $ctr = 0;
while(1){
my ($data, $ret);
$ret = sysread $file, $data, 1;
last if $ctr ++ >= 20;
print "ret : $ret, data : ", unpack("H2", $data), "\n";
}
--------------------------------------------
and file "test.txt":
--------------------------------------------
line1
line2
--------------------------------------------
and the output is:
--------------------------------------------
ret : 1, data : 6c
ret : 1, data : 69
ret : 1, data : 6e
ret : 1, data : 65
ret : 1, data : 31
ret : 1, data : 0d
ret : 1, data : 0a
ret : 1, data : 6c
ret : 1, data : 69
ret : 1, data : 6e
ret : 1, data : 65
ret : 1, data : 32
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
--------------------------------------------
which is thought to be correct.
*My question is*: when I unquote the two lines in the code (which I
think should put
the first line["line1\n\r"] into $first_line, and sysread will only get
the second line),
sysread() cannot get anything. The output is just:
--------------------------------------------
first_line is : line1
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
ret : 0, data :
--------------------------------------------
Is there anything wrong with my codes ?
Peter
Thread Next
-
Problem with sysread()
by Peter Xu