ch16.016_best_ex16.6

来自「Perl Best Practices the source code」· 6 代码 · 共 90 行

6
90
字号
################################################################################   Example 16.6 (Recommended) from Chapter 16 of "Perl Best Practices"    ####     Copyright (c) O'Reilly & Associates, 2005. All Rights Reserved.      ####  See: http://www.oreilly.com/pub/a/oreilly/ask_tim/2001/codepolicy.html  #################################################################################  Example 16-6. Avoiding name collisions in constructor arguments# Standard modules...use strict;use warnings;use IO::Prompt;use Carp;use English qw( -no_match_vars );use Data::Alias;use Readonly;package Client;use Class::Std::Utils;{    my %client_num_of;    # Every client has an ID number    my %name_of;     sub new {        my ($class, $arg_ref) = @_;         my $new_object = bless anon_scalar(), $class;            # Initialize this class's attributes with the appropriate argument set...        $client_num_of{ident $new_object} = $arg_ref->{'Client'}{client_num};        $name_of{ident $new_object}       = $arg_ref->{'Client'}{client_name};         return $new_object;    }     # etc.    END {         use Data::Dumper 'Dumper';        warn Dumper [ \%client_num_of, \%name_of ];    }} package Client::Corporate;use base qw( Client );use Class::Std::Utils;{    my %client_num_of;     # Corporate clients have an additional ID number    my %corporation_of;    my %position_of;      sub new {        my ($class, $arg_ref) = @_;         my $new_object = $class->SUPER::new($arg_ref);        my $ident = ident($new_object);         # Initialize this class's attributes with the appropriate argument set...        $client_num_of{$ident}  = $arg_ref->{'Client::Corporate'}{client_num};        $corporation_of{$ident} = $arg_ref->{'Client::Corporate'}{corp_name};        $position_of{$ident}    = $arg_ref->{'Client::Corporate'}{position};         return $new_object;    }     # etc.    END {         use Data::Dumper 'Dumper';        warn Dumper [ \%corporation_of, \%position_of ];    }} # and later... my $new_client     = Client::Corporate->new( {          'Client' => {               client_num  => '124C1',               client_name => 'Humperdinck',          },          'Client::Corporate' => {               client_num  => 'F_1692',               corp_name   => 'Florin',               position    => 'CEO',          },      });

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?