Changes between Version 43 and Version 44 of Perl
- Timestamp:
- 06/24/07 14:12:22 (17 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Perl
v43 v44 710 710 == AUTOLOAD == 711 711 * 存在しないメソッドをコールしたときに呼ばれるメソッドの定義ができる 712 * 以下は,これを利用した getter/setter の実装 712 713 {{{ 713 714 #!perl … … 716 717 717 718 package Hoge; 718 719 our $AUTOLOAD;720 719 721 720 sub new { … … 723 722 } 724 723 725 sub huge {726 my $self = shift;727 return "huge";728 }729 730 724 sub AUTOLOAD { 731 my $self = shift; 732 ( my $method = $AUTOLOAD ) =~ s/.*://g; 733 return "AUTOLOAD: $method"; 734 } 725 my $self = shift; 726 my $value = shift; 727 my $method = our $AUTOLOAD; 728 729 $method =~ s/.*:://o; 730 731 if ( $method =~ /^get_(.+)/ ) { 732 return $self->{$1}; 733 } 734 elsif ( $method =~ /^set_(.+)/ ) { 735 $self->{$1} = $value; 736 return; 737 } 738 elsif ( $method eq 'DESTROY' ) { 739 return; 740 } 741 742 die sprintf( "Can't locate object method \"%s\" via package \"%s\" at %s line %u.\n", 743 $method, __PACKAGE__, __FILE__, __LINE__ ); 744 } 745 735 746 736 747 package main; 737 748 738 my $hoge = new Hoge(); 739 print $hoge->hoge(), "\n"; 740 print $hoge->huge(), "\n"; 749 use Data::Dumper; 750 751 if ( __FILE__ eq $0 ) { 752 exit( &main(@ARGV) ); 753 } 754 755 sub main { 756 my $hoge = new Hoge(); 757 758 $hoge->set_hoge("unko"); 759 print $hoge->get_hoge(), "\n"; 760 print Dumper($hoge); 761 762 return 0; 763 } 741 764 }}} 742 765 * 実行結果 743 766 {{{ 744 $ perl Hoge.pm 745 AUTOLOAD: hoge 746 huge 767 #!sh 768 $ perl ./hoge.pl 769 unko 770 $VAR1 = bless( { 771 'hoge' => 'unko' 772 }, 'Hoge' ); 747 773 }}} 748 774