perl面向对象小练习

前端之家收集整理的这篇文章主要介绍了perl面向对象小练习前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

#!/usr/bin/perl
#编写一个面向对象的程序,对设备状态进行统计管理
package DEVICE;
sub new {
        my $class=shift;
        my $ref={
                ip_addr=>undef,
                hw_type=>undef,
                mac_addr=>undef,
                flag_mask=>undef,
                iface=>undef,
        };
        bless ($ref,$class);
}
sub set {
        my $self=shift;
        my ($ip_addr,$hw_type,$mac_addr,$iface)=@_;
        $self->{ip_addr}=$ip_addr;
        $self->{hw_type}=$hw_type;
        $self->{flag_mask}=$flag_mask;
        $self->{iface}=$iface;
}

sub display {
        my $self=shift;
        print "---------------------------------------\n";
        print "IP ADDRESS IS ".$self->{ip_addr}."\n";
        print "HARDWARE TYPE IS ".$self->{hw_type}."\n";
        print "FLAG MASK IS ".$self->{flag_mask}."\n";
        print "INTERFACE IS ".$self->{iface}."\n";
        print "---------------------------------------\n";
}

package main;
        open DF,"<./device";
        my $i=0;
        while (<DF>){
                my @tmp= split(" ",$_);
                ${dev_{$i}}=DEVICE->new();
                ${dev_{$i}}->set(@tmp);
                $i++;
                print  $i."\n";
        }
my $j;
for ($j=0;$j<$i;$j++){
        ${dev_{$j}}->display;
}

说明:device文件位置及内容设置参考下图:

 

猜你在找的Perl相关文章