天天看點

第一章 輸入/輸出知識

1.3 标準檔案句柄:

243[oracle@oadb 1]$ cat data.txt 
234
[oracle@oadb 1]$ cat muncher.pl 
while (<STDIN>){
  print $_ + 9;
};
[oracle@oadb 1]$ cat data.txt 
234
[oracle@oadb 1]$ perl muncher.pl  <data.txt >a.txt
[oracle@oadb 1]$ cat a.txt 
243[oracle@oadb 1]$ 

1.3.2 輸入和輸出操作:

[oracle@oadb 1]$ cat a2.pl 
open (A,"aa");
$line=<A>;
print $line."\n";
[oracle@oadb 1]$ perl a2.pl 
1234567

[oracle@oadb 1]$ cat a3.pl 
open (A,"aa");
@line=<A>;
print @line;
[oracle@oadb 1]$ perl a3.pl 
1234567
abcdefg

[oracle@oadb 1]$ cat bb
9999999999
8888888888
[oracle@oadb 1]$ perl a4.pl  aa bb
$_ is 1234567

$_ is abcdefg

$_ is 9999999999

$_ is 8888888888



$bytes=read(FILEHANDLE,$buffer,$length [,$offset])

$bytes=sysread(FILEHANDLE,$buffer,$length  [,$offset])

[oracle@oadb 1]$ perl a5.pl
1234567890
$bytes is 7
$buffer is 1234567
[oracle@oadb 1]$ cat a5.pl 
my $buffer;
$bytes=read(STDIN,$buffer,7);
print "\$bytes is $bytes\n";
print "\$buffer is $buffer\n";
[oracle@oadb 1]$ 

預設時,所讀取的資料将被放在$buffer的開始部分,覆寫$buffer中原有的内容。

可以通過可選的數值型參數$offset來改變函數的這種行為,指定所讀物的資料寫入可選的被指定的開始位置。

[oracle@oadb 1]$ cat a5.pl 
my $buffer="1234567";
$bytes=read(STDIN,$buffer,5,3);
print "\$bytes is $bytes\n";
print "\$buffer is $buffer\n";
[oracle@oadb 1]$ perl a5.pl 
abcdefg1234567
$bytes is 5
$buffer is 123abcde

read()和sysread()的差別:

read()讀取所需的實際位元組數或讀到檔案尾才傳回

sysread()函數會傳回部分資料

[oracle@oadb 1]$ cat a5.pl 
my $buffer="1234567";
$bytes=read(STDIN,$buffer,30,3);
print "\$bytes is $bytes\n";
print "\$buffer is $buffer\n";
[oracle@oadb 1]$ perl a5.pl 
abcdefg

此時不傳回資料


[oracle@oadb 1]$ cat a5.pl 
my $buffer="1234567";
$bytes=sysread(STDIN,$buffer,30,3);
print "\$bytes is $bytes\n";
print "\$buffer is $buffer\n";
[oracle@oadb 1]$ perl a5.pl 
abcdefg
$bytes is 8
$buffer is 123abcdefg

[oracle@oadb 1]$ 


$bytes=syswrite(FILEHANDLE,$data [,$length [,$offset]]);

1.3.3 檢測檔案尾



1.3.4 行尾之處的混亂:

在UNIX 系統上,行以換行符(LF \n)結尾

Windows 上是\r\n


1.3.5 打開和關閉檔案:

[oracle@oadb 1]$ cat a7.pl 
open (FH,">message.txt") or die $!;
print FH "this is the first line.\n";
print FH "And this is the second line.\n";

1.3.6 緩沖和堵塞


1.4 使用IO::Handle子產品和IO::File子產品的面向對象文法:



在本書的後面我們會使用perl5 的面向對象的擴充功能,盡管你不需要知道太多建立面向對象子產品的知識,

但你需要基本了解如何使用面向對象子產品和它們的文法。

1.4.1 對象和引用:

[oracle@oadb 1]$ cat a1.pl 
my @array=(1,4,7,2,5,8);
my $array=\@array;
print "----------------\n";
print $array;
print "\n";
print "----------------\n";
print $array->[2];
print "\n";
print $$array[3];
print "\n";

[oracle@oadb 1]$ perl a1.pl 
----------------
ARRAY(0x9d95b8)
----------------
7
2


對象(object)也是一個引用,隻不過具有一些額外的位。

對象帶有"是什麼子產品建立它"的相關資訊,它以這種方式被"神聖化"(blessed)而居于特定子產品的軟體包中。

$object->print_record();##調用print_record()方法

你可能有時會看到一個方法被調用時具有參數,像下面這樣:

負責将一般引用轉化為"神聖化"的引用的函數自然被稱為bless()

$object->{last_name};

使用對象不同于一般引用的是它們具有方法(method),方法調用使用->符号,但後面緊跟着一個子例程名和可選的子例程的子例程風格的參數:


$object->print_record();##invoke the print_record() 方法

你可能有時看到一個方法被調用時具有參數,像下面這樣:


$object->print_record(encoding=>'EBCDID');

[oracle@oadb 1]$ cat BigDatabase.pm 
package BigDatabase;
our $VERSION="2.0";
sub new{
   my $class=shift;
   my %p=@_;
   my $self={
    version => $VERSION,
    debug => $p{debug}
};
   bless $self,$class;
   return $self;
};


sub print_record{
   my $self=shift;
   my %p=@_;
   my $var=$p{data};
   return $self->{debug} + $var;
};
1;

[oracle@oadb 1]$ cat a2.pl 
use BigDatabase;
my $object=BigDatabase->new(debug=>890);
print $object->print_record(data=>56);
print "\n";
print "------------------------------\n";
print BigDatabase::print_record($object);
print "------------------------------\n";
[oracle@oadb 1]$ perl a2.pl 
946
------------------------------
890------------------------------


為了建立對象,必須調用它的一個構造函數(constructor)。構造函數是一個從子產品名調用的方法調用,

例如,建立一個新的BigDatabase對象,代碼如下:


$object=BigDatabase->new(); ##call the new() 構造器

構造函數是一個類方法(class method)的特例,經常被命名為new(). 然而,任意的子例程名都是可能的,

1.4.2 IO::Handle子產品和IO::File子產品