zoukankan      html  css  js  c++  java
  • 第一章 输入和输出基础

    [root@dwh1 perl]# cat 1.pl 
    #!/usr/bin/perl
    #file:lgetl.pl


    ##装入模块,我们通过use()使用IO::File模块,该模块包含Perl文件操作的面向对象接口


    use IO::File;


    #处理命令行参数 我们通过shift()从命令行中取出文件名并将其存储在名为$file的变量中


    my $file = shift;


    ##我们调用IO::File ->new()方法来打开文件,返回一个文件句柄,存储在$fh中。


    ##如果你不熟悉面向对象的语法也不要着急
    my $fh = IO::File->new($file);


    my $line = <$fh>;


    print $line;




    [root@dwh1 perl]# cat 2.pl
    $input = <STDIN>;
    chomp ($input);
    print STDOUT "$input ";




    1.3.2  输入和输出操作:


     如何从文件句柄中读取数据,Perl给出了两种选择:一种是一次读取一行,适用于文本文件。


    另一种是读取任意长度的数据块,适用于图像文件的2进制字节流






    use Errno qw (EACCES ENOENT);
    my $result = open (FH,"</var/log/messages");
    if (!$result){#oops,something went wrong
     print "$! is $! ";
     if ($! == EACCESS) {
      warn "You do not have permission to open this file.";
    }elsif ($! == ENOENT) {
      warn "File or directory not found.";
    }else {
    warn "Some other error occurred:$!"
     }
    }


    1.4.1  对象和引用:


     在Perl中,引用(reference)是指向数据结构的指针。你可以使用反斜杠运算符来创建已存在的数据结构的引用。例如:


    [oracle@dwh1 perl]$ cat 2.pl  
    $a = 'hi there';
    $a_ref = $a; ##reference to a scalar




    print "$a_ref is $$a_ref ";


    @b = ('this','is','an','array');




    $b_ref = @b;  #reference to an array




    print "$b_ref is @$b_ref ";




    %c = (first_name =>'Fred',last_name => 'Jones');


    $c_ref = \%c; ###reference to a hash




    print %$c_ref;
    print " ";


    ##-------------------------------------------------------
    $a = $b_ref->[2];  ##yields "an"


    $b = $c_ref->{last_name};  ##yields "Jones"




    print "$a is $a ";


    print "$b is $b ";


    [oracle@dwh1 perl]$ perl 2.pl
    $a_ref is SCALAR(0x2019560)
    $a_ref is hi there
    $b_ref is this is an array
    last_nameJonesfirst_nameFred
    $a is an
    $b is Jones


    如果你打印一个引用,你将获得一个类似SCALAR(0x2019560)的字符窜,该字符串指示引用的类型


    和该引用在内存中的位置(没什么用处).


      对象(object)也是一个引用,只不过具有一些额外的位,对象带有"是什么模块创建它"的相关信息,它以这种方式被"神圣化"(blessed)而


    居于特定模块的软件包中。 这些"神圣的"引用将继续和其他引用一样工作。例如,如果一个名为$object的对象时一个神圣化的


    散列数组引用,你可按如下所示索引它:


    #!/usr/bin/perl
    #
    use strict;


    ##装入IO::File模块
    use IO::File;


    ##从命令行中取到要执行行计数的文件的名字并将$counter 变量初始化为0
    my $file = shift;


    my $counter = 0;
    my $fh = IO::File->new ($file) or die;


    ####$fh 是对象的引用
    print "$fh is $fh ";
    while (defined (my $line = $fh->getline)){
      $counter++;
      }
    STDOUT->print("Counted $counter lines ")




    H:20150226Socket>perl 1.pl 1.txt
    $fh is IO::File=GLOB(0x202e0bc)
    Counted 202 lines




    open (FH,"<1.txt") or die;
    $line = <FH>;
    print "$line is $line ";
    close (FH);


    open (FH,"<1.txt") or die;
    @lines = <FH>;
    print "@lines is @lines ";
    close (FH);


    $a = <>;
    print "$a is $a "; 


    @a = <>;
    print "@a is @a ";








    [oracle@dwh1 perl]$ cat a5.pl 
    open (FH,"<1.txt") or die;
     while (<FH>) {
      print "Found $_ ";
    }
    [oracle@dwh1 perl]$ perl a5.pl 
    Found 11111111


    Found 22222222


    Found 33333333


    <FILEHANDLE> 形式显示地给出读取数据的文件句柄。


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


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


















































































  • 相关阅读:
    遗传算法(Genetic Algorithm, GA)及MATLAB实现
    CCF CSP 201809-2 买菜
    PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题 (20 分)
    PAT (Basic Level) Practice (中文)1006 换个格式输出整数 (15 分)
    PAT (Basic Level) Practice (中文)1004 成绩排名 (20 分)
    PAT (Basic Level) Practice (中文)1002 写出这个数 (20 分)
    PAT (Advanced Level) Practice 1001 A+B Format (20 分)
    BP神经网络(原理及MATLAB实现)
    问题 1676: 算法2-8~2-11:链表的基本操作
    问题 1744: 畅通工程 (并查集)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351836.html
Copyright © 2011-2022 走看看