ch16.040_best

来自「Perl Best Practices the source code」· 040_BEST 代码 · 共 59 行

040_BEST
59
字号
################################################################################   Code fragment (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  ################################################################################# Standard modules...use strict;use warnings;use IO::Prompt;use Carp;use English qw( -no_match_vars );use Data::Alias;use Readonly;BEGIN {Readonly my $COMMA => q{,};package Coerced;use Class::Std;{    my %elements_of : ATTR( init_arg => 'elem' );    # Call count() method whenever object used as number    sub count : NUMERIFY {           my ($self, $ident) = @_;        return scalar @{ $elements_of{$ident} };     }        # Call as_str() method whenever object used as string    sub as_str : STRINGIFY {         my ($self, $ident) = @_;        return sprintf '(%s)', join $COMMA, @{ $elements_of{$ident} };     }        # Call is_okay() method whenever object used as boolean    sub is_okay : BOOLIFY {         my ($self) = @_;        return !$self->Houston_We_Have_A_Problem();    }    sub Houston_We_Have_A_Problem {        return 1;    }}}my $coercable = Coerced->new({elem => [10..17]});print "str: '$coercable'\n";print "num: ", 0+$coercable, "\n";if (!$coercable) {    print "We have a problem (as expected)\n"}

⌨️ 快捷键说明

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