zoukankan      html  css  js  c++  java
  • perl 面向对象编程

    今天看到一个perl面向对象编程的例子,充分体现了如何对数据进行封装;

    自己模仿写一个读取配置文件的例子, 配置文件的内容如下

    samtools_binary = /usr/bin/samtools
    

    用=分隔,保存了每个软件可执行文件的绝对路径;

    下面用perl 模块进行配置文件的读取, 模块名为config.pm

    package config;
    
    use strict;
    use warnings FATAL => 'all';
    
    sub new 
    {
        my $self = {};
        
        bless($self);
    
        return $self;
    }
    
    sub read 
    {
        my $self = shift;
        my $config_filename = shift;
    
        my %config_values;
    
        open CFG, $config_filename or die "Error: Unable to open $config_filename
    ";
        while (<CFG>) 
        {
            chomp;
            next if /^#/;
            next if /^s*$/;
    
            my ($key, $value) = split /=/, $_, 2;
    
            next if not defined $key;
            next if not defined $value;
    
            $key   =~ s/^s*(S+)s*$/$1/;
            $value =~ s/^s*(S+)s*$/$1/;
    
            $config_values{$key} = $value;
        }
        close CFG;
        $self->{'config_values'}   = %config_values;
        $self->{'config_filename'} = $config_filename;
    }
    
    sub get_value {
        my $self = shift;
        my $key  = shift;
    
        my $config_values   = $self->{'config_values'};
        my $config_filename = $self->{'config_filename'};
    
        defined $config_values->{$key} or die "Error:no values for $key at $config_filename
    ";
        return $config_values->{$key};
    }
    
    1;

    然后在脚本中调用这个模块

    #!/usr/bin/perl
    
    use lib qq{./};
    use config;
    
    my $config = config->new;
    my $config_filename = qq{config.txt};
    $config->read($config_filename);
    print $config->get_value(qq{samtools_binary});

    在config.pm中,可以将config看作一个类,而new函数返回对该类的一个对象的引用;

    在read函数中,通过在对象和我们真正想要读取的值之间添加一层哈希,对数据进行封装;

    在看不到源代码的情况下,只能通过指定的get_value 方法读取配置文件中参数的值;

    如果写成下面这样,就实现不了封装的效果

    package config;
    
    use strict;
    use warnings FATAL => 'all';
    
    sub new 
    {
        my $self = {};
        
        bless($self);
    
        return $self;
    }
    
    sub read 
    {
        my $self = shift;
        my $config_filename = shift;
    
        my %config_values;
    
        open CFG, $config_filename or die "Error: Unable to open $config_filename
    ";
        while (<CFG>) 
        {
            chomp;
            next if /^#/;
            next if /^s*$/;
    
            my ($key, $value) = split /=/, $_, 2;
    
            next if not defined $key;
            next if not defined $value;
    
            $key   =~ s/^s*(S+)s*$/$1/;
            $value =~ s/^s*(S+)s*$/$1/;
    
            $self->{$key} = $value;
        }
        close CFG;
        $self->{'config_filename'} = $config_filename;
    }
    
    sub get_value {
        my $self = shift;
        my $key  = shift;
    
    
        defined $self->{$key} or die "Error:no values for $key
    ";
        return $self->{$key};
    }
    
    1;

      

  • 相关阅读:
    oracle数据库导出与导入
    Mysql导入表信息[Err] 1067
    Golang--不定参数类型
    (转)Docker容器的重启策略及docker run的--restart选项详解
    (转)Golang--使用iota(常量计数器)
    Golang--匿名变量
    Golang--Hello World
    Ubuntu Server16.04 配置网卡
    U盘安装ubuntu 16.04 遇到 gfxboot.c32:not a COM32R image boot 的解决方法
    ipfs私链服务
  • 原文地址:https://www.cnblogs.com/xudongliang/p/5145700.html
Copyright © 2011-2022 走看看