Perl5ã§ã®ãªãã¸ã§ã¯ãã®åºç¤ã解説ãããã¨æãã¾ãã
Perlã§ã®ãªãã¸ã§ã¯ãæåã¨è¨ãã°ãã¢ãã³Perlç³»ã§ã¯Mooseã¢ã¸ã¥ã¼ã«çãªã®ã§ãããä»åã¯ãããã¾ã§ãåºç¤ç·¨ã§ãã
Perlã§æéã«ãªãã¸ã§ã¯ãããã¹ã¿ã¼ããã«ã¯ãã¾ããããªãã¡ã¬ã³ã¹ãã¨ãuse宣è¨ãã¨ãblessãã®ï¼ã¤ãç解ã§ãã¦ããªãã¨ãªãã¾ããã
ãªããã¨è¨ãã¨ãPerlã§ã®ãªãã¸ã§ã¯ãã¯blessããããªãã¡ã¬ã³ã¹ãªã®ã§ããã¾ããPerlã§ã®ã¯ã©ã¹ã¯ã¢ã¸ã¥ã¼ã«ãªã®ã§ãããã®ã¢ã¸ã¥ã¼ã«ã§ã®ä¸çªã®è¬ã¯ãuse宣è¨ãªã®ã§ãã
ãªã®ã§ãã¯ããã«ãã®ï¼ã¤ãç解ãã¦ããããã®ãã¨ããªãã¸ã§ã¯ããã¨ããªãã¸ã§ã¯ãæåãã«ã¤ãã¦è§£èª¬ãã¦ããã¾ãã
ã¤ã¾ãã以ä¸ã®é çªã§ãã
- ãªãã¡ã¬ã³ã¹ã«ã¤ãã¦
- use宣è¨ã«ã¤ãã¦
- blessã«ã¤ãã¦
- Perlã§ã®ãªãã¸ã§ã¯ãã«ã¤ãã¦
- Perlã§ã®ãªãã¸ã§ã¯ãæåã«ã¤ãã¦
â ãªãã¡ã¬ã³ã¹ã«ã¤ãã¦
æ®éããªãã¡ã¬ã³ã¹ã¨ããã°ãä¸è¦§ã®æ©è½åç
§ã®ãã¨ã§ãããPerlã§ãªãã¡ã¬ã³ã¹ã¨ããã°ãè¨èªä»æ§ã®ã¢ãã¬ã¹åç
§ã®ãã¨ãªã®ã§ãã
ã¾ããPerlã§ã®ãªãã¡ã¬ã³ã¹ãï¼£è¨èªã§ããã°ãã¢ãã¬ã¹æ¼ç®ãã§ããªããã¤ã³ã¿ã®ãããªãã®ãªã®ã§ãã
ãã¨ãã°ãé
åå¤æ° @array ã®ãªãã¡ã¬ã³ã¹ï¼åç
§ï¼ã¯ã \æ¼ç®åã§åå¾ãããã¨ãã§ãã¾ãã
以ä¸ã¯ãé
åå¤æ° @array ã®ãªãã¡ã¬ã³ã¹ãã¹ã«ã©ã¼å¤æ° $array_ref1 ã¸æ ¼ç´ãã¦ãã¾ãã
my @array = (1,2,3); print $array[2], "\n"; # 3çªç®ã®è¦ç´ ã表示 my $array_ref1 = \@array; # é åå¤æ°ã®ãªãã¡ã¬ã³ã¹ãã¹ã«ã©ã¼å¤æ°ã¸æ ¼ç´
ã¤ãã«ãªãã¡ã¬ã³ã¹ããå
ã®å¤ãæ±ãããæã¯ãããªãã¡ã¬ã³ã¹ããã¨è¨ãã¾ãã
ããªãã¡ã¬ã³ã¹ã«ã¯ãåç
§å
ã®å¤æ°ã®èå¥åãå
é ã«ä»ã足ãæ¹æ³ã¨ç¢å°æ¼ç®åã->ãã使ãæ¹æ³ã®ï¼éããããã¾ãã
print $$array_ref1[2], "\n"; # 3çªç®ã®è¦ç´ ã表示; print $array_ref1->[2], "\n"; # 3çªç®ã®è¦ç´ ã表示
ãªãã¡ã¬ã³ã¹ã®å©ç¨å ´é¢ã¯ãç§ãæãã¤ãã¨ããã§ã¯ä»¥ä¸ã®ã±ã¼ã¹ã§ãã
- å¤æ¬¡å é åãå¦çããã±ã¼ã¹
- é¢æ°ããªãã¡ã¬ã³ã¹ã§ã³ã¼ã«ããã±ã¼ã¹
- é¢æ°ã®å¼æ°ã¸é åã渡ãã±ã¼ã¹
- é¢æ°ã®ãªã¿ã¼ã³å¤ã«ããã±ã¼ã¹
âå¤æ¬¡å é åãå¦çããã±ã¼ã¹
my $array_ref1 = \@array;
é
åå¤æ°ã®@ã®åã«ã\ãã¤ããã¨ãé
åã®ãªãã¡ã¬ã³ã¹ãåå¾ã§ãã¾ãã
ä¸è¨ã¯ãé
å@arrayã®ãªãã¡ã¬ã³ã¹ã$array_ref1ã¸æ ¼ç´ãã¦ãã¾ãã
ã¾ããç¡åé
åã®ãªãã¡ã¬ã³ã¹ãæ ¼ç´ãããã¨ãã§ãã¾ãã
my $array_ref2 = [ 1, 2, 3, 4 ];
ãªã¹ãã [ ] ã§ãããã¨ãç¡åé åã«ãªãããã®ãªãã¡ã¬ã³ã¹ã$array_ref2ã¸æ ¼ç´ãã¦ãã¾ãã
ã¤ãã«ããªãã¡ã¬ã³ã¹ããå ã®å¤ãåå¾ãããã¨ãããªãã¡ã¬ã³ã¹ã¨ããã¾ãã
my $array_ref2 = [ 1, 2, 3, 4 ]; print $array_ref2->[3]; #ããªãã¡ã¬ã³ã¹
ã¡ãªã¿ã«ãã->ãã¯ç¢å°æ¼ç®åã¨ããããªãã¡ã¬ã³ã¹ãããªãã¡ã¬ã³ã¹ãã¾ãã
è£è¶³ï¼ãªãã¡ã¬ã³ã¹ã®å
ããªãã¡ã¬ã³ãããã¾ãã
ã¤ã¾ãã$array_ref1ããªãã¡ã¬ã³ã¹ã§ã\@arrayããªãã¡ã¬ã³ãã§ãã
ãã®æ©è½ãå©ç¨ããã¨å¤æ¬¡å
é
åã®å¦çãå¯è½ã§ãã
以ä¸ã¯ãé
åå¤æ° @a ã®è¦ç´ ã«é
åã®ãªãã¡ã¬ã³ã¹ãåã
æ ¼ç´ãã¦ãã¦ãçµæçã«ï¼æ¬¡å
é
åã«ãã¦ãã¾ãã
@a = ( [ qw(00 01 02) ], [ qw(10 11 12) ], [ qw(20 21 22) ]); for $i (0 .. $#a) { for $j (0 .. $#{$a[$i]}) { print '$a[' . $i . '][' . $j . ']=' . $a[$i][$j] . ' '; } print "\n"; }
å°ãä¸è¨ã§ã¯ã->ãç¢å°æ¼ç®åãããã¾ãããã大æ¬å¼§ï¼ãã©ã±ããï¼ãä¸æ¬å¼§ï¼ãã¬ã¼ã¹ï¼ã®éã«ã¯ãã¾ããç¢å°ã¯çç¥å¯è½ãªã®ã§ãã
âé¢æ°ããªãã¡ã¬ã³ã¹ã§ã³ã¼ã«ããã±ã¼ã¹
é¢æ°ã®ãªãã¡ã¬ã³ã¹ãé åã¨åæ§ã«ã§ãã¾ãã
sub01("aaa"); # æ®éã«ã³ã¼ã« $sub = \&sub01; $sub->("aaa"); # ãªãã¡ã¬ã³ã¹ã§ã³ã¼ã« sub sub01 { print "--- sub01 $_[0]---\n"; }
ã$sub = \&sub01;ãã§é¢æ°ã®ãªãã¡ã¬ã³ã¹ãæ ¼ç´ãã¦ãã$sub->("aaa")ãã§ã³ã¼ã«ãã¦ãã¾ãã
âé¢æ°ã®å¼æ°ã¸é åã渡ãã±ã¼ã¹
Perlã§ã®é¢æ°ã®å¼æ°ã¯ãªã¹ãã§ãã
å¼æ°ã«é
åå¤æ°ã渡ãã¨ãªã¹ãã«å±éããã¦ãã¾ããæ®éã¯ä¸æãé
åå¤æ°ã渡ãã¾ããã
ããã§ç»å ´ãã¦ããã®ãé
åå¤æ°ã®ãªãã¡ã¬ã³ã¹ã§ãã
ãªãã¡ã¬ã³ã¹ãªããä¸æãé
åå¤æ°ã渡ããã®ã§ãã
my @bbb = ("bbb", "ccc", "ddd"); my @xxx = ("xxx", "yyy", "zzz"); sub02("aaa", \@bbb, \@xxx); sub sub02 { print "--- sub02 $_[0]---\n"; print "--- sub02 $_[1]->[0]---\n"; print "--- sub02 ${$_[1]}[1]---\n"; print "--- sub02 $_[1][2]---\n"; print "--- sub02 $_[2][0]---\n"; print "--- sub02 $_[2][1]---\n"; print "--- sub02 $_[2][2]---\n"; }
â é¢æ°ã®ãªã¿ã¼ã³å¤ã«ããã±ã¼ã¹
é¢æ°ã®ãªã¿ã¼ã³å¤ã§blessãããªãã¡ã¬ã³ã¹ãè¿ãããå ´åãããã¯ãä»è¨èªã§ãããªãã¸ã§ã¯ãã¨åãæãã«ãªãã®ã§ãã
my $date = DateString->new(); print $date->to_string, "\n"; package DateString; sub new { my $class = shift; # 第ä¸å¼æ°ã¯ãã¯ã©ã¹åã渡ãããã my $time = shift || time(); return bless { time => $time }, $class; # blessãããªãã¡ã¬ã³ã¹ãè¿ãã } sub to_string { my $self = shift; return scalar localtime $self->{time}; }
â use宣è¨ã«ã¤ãã¦
Perlãããå§ããã¨ãrequireã¯ããåãããã ããã©ãuseã£ã¦è¬ã ãªãã£ã¦æãã¾ããï¼
ä»åã¯ããããªuseã®è¬ã«è¿«ãã¾ãã
ã¯ããã«ãrequireã¯ãï¼£è¨èªã§ãã%includeã¨åãã§å¥ãã¡ã¤ã«ã«ããã½ã¼ã¹ãå®è¡æã«ããããèªã½ã¼ã¹ã¨ãã¦èªã¿è¾¼ã¿ã¾ãã
ã¤ãã«ãuseã¯ãå®è¡åï¼ã³ã³ãã¤ã«æï¼ã«ããããèªã½ã¼ã¹ã¨ãã¦èªã¿è¾¼ã¿ã¾ãã
å®è¡åã¨ããã®ã¯ãPerlã§ã¯ã³ãã³ãèµ·åæã«ã³ã³ãã¤ã«ãã¦ããå®è¡ããã¨ããï¼æ®µéæ¹å¼ã«ãªã£ã¦ããã®ã§ãã
ã¤ã¾ããuseã¯ã³ã³ãã¤ã«æã«å®è¡ãããrequireã¯ã³ã³ãã¤ã«å¾ã«å®è¡ãããã®ã§ãã
ããã¦ãèªã¿è¾¼ãã ã½ã¼ã¹ï¼ã¢ã¸ã¥ã¼ã«ï¼ã«package宣è¨ããªãå ´åãuseã¨requireã§ã¯ããã以å¤ã®éãã¯ããã¾ããã
ããããã¢ã¸ã¥ã¼ã«ã«package宣è¨ãããã¨ãã¡ãã£ã¨ããã£ããããªãã®ã§ãã
ã¾ããã¢ã¸ã¥ã¼ã«ã§ãªããã¡ã¤ã³ã½ã¼ã¹ã®æ¹ã«ã¯ãpackage宣è¨ãããã¾ããããå®ã¯ãããã©ã«ãã§mainã¨ããpackageåã«ãªã£ã¦ãã¾ãã
ã¤ã¾ããéã«è¨ãã¨ãpackage宣è¨ã®ãªãã¢ã¸ã¥ã¼ã«ã¯ãmainã¢ã¸ã¥ã¼ã«ãªã®ã§ãã
ä¸æ¹ãã¢ã¸ã¥ã¼ã«ã®æ¹ã¯ãpackage宣è¨ã§packageåãmain以å¤ã§æå®ããã¨ãå½ç¶ã§ããã¡ã¤ã³ã½ã¼ã¹ã¨ã¯å¥ã®packageåã«ãªãã¾ããã
ããããå¥ã®packageåã«ãªã£ã¦ããuseã«ã¯ä»ã¢ã¸ã¥ã¼ã«ããããããèªã¢ã¸ã¥ã¼ã«ï¼mainã¢ã¸ã¥ã¼ã«ï¼ã«ããããã«ããä»çµã¿ãããã®ã§ãã
ãã¨ãã°ã以ä¸ã®ãµã³ãã« mainã¢ã¸ã¥ã¼ã«ã¨Moduleã¢ã¸ã¥ã¼ã« ãã覧ä¸ããã
- ãã®ä»çµã¿ã¯ãModuleã¢ã¸ã¥ã¼ã«ã®importã¡ã½ããã§ãã
- ãuse Moduleãã¯ãå®ã¯ãBEGIN { require Module; Module->import; }ãã¨ã¤ã³ã¼ã«ãªã®ã§ãã
- ãBEGINãã¯ãã³ã³ãã¤ã«æã«å®è¡ããããããã¯ãªã®ã§ãã
- ãModule->import;ãã¯ãModuleã¢ã¸ã¥ã¼ã«ã«ãã sub ãmainã¢ã¸ã¥ã¼ã«ã«ã¤ã³ãã¼ããããã®ãªã®ã§ãã
ãããªããimportãåºã¦ãã¾ãããããã¯ãModuleã¢ã¸ã¥ã¼ã«å
ã§Exporterã¢ã¸ã¥ã¼ã«ãç¶æ¿ãã¦ãã¦ãExporterã¢ã¸ã¥ã¼ã«å
ã®ã¡ã½ãããªã®ã§ãã
âãµã³ãã«
ã»mainã¢ã¸ã¥ã¼ã«
#use Module; BEGIN { require Module; Module->import;} # use Module; ã¨ç価 print sumx(5,3),"\n"; # Moduleã¢ã¸ã¥ã¼ã«ã®sumxé¢æ°ãããããèªé¢æ°ã®ããã«ä½¿ãã
ã»Moduleã¢ã¸ã¥ã¼ã«
package Module; use base Exporter; # use Exporter; our @ISA = qw(Exporter); ã¨ç価 our @EXPORT = qw(&sumx); sub sumx { return $_[0]+$_[1]; # 渡ãããå¼æ°ï¼ã¤ãå ãã¦è¿ãå¦ç } 1;
âè£è¶³
Moduleã¢ã¸ã¥ã¼ã«ã§ã¯ããuse base Exporterãã§Exporterãç¶æ¿ãã¦ããour @EXPORT = qw(&sumx);ãã§sumxé¢æ°ãã¨ã¯ã¹ãã¼ããã¦ãã¾ãã
mainã¢ã¸ã¥ã¼ã«ã§ã¯ããModule->import;ãã§Moduleã¢ã¸ã¥ã¼ã«ã§ã¨ã¯ã¹ãã¼ãããsumxé¢æ°ãã¤ã³ãã¼ããã¦ãã¾ãã
â blessã«ã¤ãã¦
useãã¢ã¸ã¥ã¼ã«ã¨ãªãã¡ã¬ã³ã¹ãç解åºæ¥ã¦ããã¨ãblessãæ°ã«ãªã£ã¦ãã¾ããï¼
ä»åã¯ããããªblessã®è¬ã«è¿«ãã¾ãã
blessãä¸è¨ã§ããã¨ããªãã¡ã¬ã³ã¹ã¨ããã±ã¼ã¸ãç´ä»ãã¾ãã
ãã¨ãã°ã以ä¸ãè¦ã¦ãã ããã
use strict; use warnings; my %test_HASH = (aaa => 'XXX', bbb =>'YYY'); my $test = bless \%test_HASH, 'package01'; # ãªãã¡ã¬ã³ã¹ã¨ããã±ã¼ã¸ãç´ä»ã print '-->', $test->{aaa}, '<--', "\n"; $test->test01(3); print '-->', $test->{aaa}, '<--', "\n"; package package01; sub test01 { print "第ï¼å¼æ°ã¯$_[0], 第ï¼å¼æ°ã¯$_[1]ã§ãã", "\n"; my $obj_self = shift; $obj_self->{aaa} = 'ZZZ'; }
ãmy $test = bless \%test_HASH, 'package01';ãã§ã%test_HASHã®ãªãã¡ã¬ã³ã¹ã¨package01ã®ããã±ã¼ã¸ãç´ä»ãã¦ãã¾ãã
ããã¦ãblessã®ãã¿ã¼ã³å¤ã%test_HASHã®ãªãã¡ã¬ã³ã¹ãªã®ã§ãããã ã ã®ãªãã¡ã¬ã³ã¹ã§ãªããblessã§ç´ä»ããããpackage01ããã±ã¼ã¸ãå«ã¾ããã®ã§ãã
ãªã®ã§ãã$test->test01(3);ãã®ããã«package01ããã±ã¼ã¸ã®test01ãµããã³ã¼ã«ãããã¨ãåºæ¥ãã®ã§ãã
ã³ã¼ã«ããæãã->ãç¢å°æ¼ç®åã使ãã¨test01ãµãã§ã¯ãèªåçã«ç¬¬ï¼å¼æ°ãèªãªãã¡ã¬ã³ã¹ã«ãªãã®ã§ãã
ãªã®ã§ãã$obj_self->{aaa} = 'ZZZ';ãã§ãªãã¡ã¬ã³ã¹ãããã¨ã®ããã·ã¥å¤æ°ã«ã¢ã¯ã»ã¹ãã¦æ¸ãæããå¯è½ãªã®ã§ãã
çµæçã«ãªãã¡ã¬ã³ã¹ããããã·ã¥å¤æ°ï¼ããããã£ï¼ã¨ãµãã«ã¼ãã³ï¼ã¡ã½ããï¼ãæã¤ãã¨ã«ãªãã®ã§ãã
ããããã£ã¨ã¡ã½ããã¨è¨ãã°ãããããªãã¸ã§ã¯ãã«ãªãã®ã§ããâ¦ãããï¼ï¼
â ãªãã¸ã§ã¯ãã«ã¤ãã¦
ãã¼ã¨ãPerlã§ã¯ä¸ã¤ã®ããã±ã¼ã¸ãä¸ã¤ã®ã¯ã©ã¹ã«ãªãã¾ãã
ã¤ã¾ããããã±ã¼ã¸åãã¯ã©ã¹åã«ãªãã®ã§ãã
ã¾ãæ®éãã¯ã©ã¹ã«ã¯ããªãã¸ã§ã¯ããæ§ç¯ããã³ã³ã¹ãã©ã¯ã¿ãããã¾ãã
ã³ã³ã¹ãã©ã¯ã¿ã¯ãblessã§ãªãã¡ã¬ã³ã¹ãã¯ã©ã¹åã«ç´ä»ãã¦ããªã¿ã¼ã³å¤ã«ãã¾ãã
ã³ã³ã¹ãã©ã¯ã¿ã®å¼ã³åºãå´ã§ã¯ããªã¿ã¼ã³å¤ãå¤æ°ã«æ ¼ç´ãã¦ãã¤ã³ã¹ã¿ã³ã¹ã¨ãã¾ãã
ã£ã¦ãæç« ã§èª¬æãã¦ãåããè¾ãã®ã§ã以ä¸ã®ã½ã¼ã¹ãè¦ã¦æãã¨ã£ã¦ãã ããã
- ãã¤ã³ãï¼ã®ä¾ã¯ãã¯ã©ã¹åã«ç¢å°æ¼ç®åã§é¢æ°ãã³ã¼ã«ããã¨ç¬¬ï¼å¼æ°ã«ã¯ã©ã¹åãèªåçã«å ¥ãã¾ãã
- ãã¤ã³ãï¼ã®ä¾ã¯ãã¤ã³ã¹ã¿ã³ã¹ã«ç¢å°æ¼ç®åã§é¢æ°ãã³ã¼ã«ããã¨ç¬¬ï¼å¼æ°ã«ãªãã¸ã§ã¯ããèªåçã«å ¥ãã¾ãã
- ãã¤ã³ãï¼ã®ä¾ã¯ããªãã¸ã§ã¯ãã®ã³ã³ã¹ãã©ã¯ã¿ï¼å§è²ï¼èµ·åãã¡ã½ããèµ·åããã¦ãã¾ãã
use strict; use warnings; print "\n", 'âãã¤ã³ãï¼ã®ä¾ï¼ããã±ã¼ã¸åï¼ã¯ã©ã¹åï¼ã第ï¼å¼æ°ï¼', "\n"; package00->test00(3); # ã¯ã©ã¹åã«ç¢å°æ¼ç®åã§é¢æ°ãã³ã¼ã«ãã package package00; sub test00 { print "第ï¼å¼æ°ã¯$_[0], 第ï¼å¼æ°ã¯$_[1]ã§ãã", "\n"; my $class = shift; print "第ï¼å¼æ°ã¯ $class ã§ã¯ã©ã¹åã«ãªãã¾ãã\n" } print "\n", 'âãã¤ã³ãï¼ã®ä¾ï¼blessãªãã¡ãªã³ã¹ï¼ãªãã¸ã§ã¯ãï¼ã第ï¼å¼æ°---', "\n"; my %test_HASH = (aaa => 'XXX', bbb =>'YYY'); my $test = bless \%test_HASH, 'package01'; print '-->', $test->{aaa}, '<--', "\n"; $test->test01(3); # ã¤ã³ã¹ã¿ã³ã¹ã«ç¢å°æ¼ç®åã§é¢æ°ãã³ã¼ã«ãã print '-->', $test->{aaa}, '<--', "\n"; package package01; sub test01 { print "第ï¼å¼æ°ã¯$_[0], 第ï¼å¼æ°ã¯$_[1]ã§ãã", "\n"; my $obj_self = shift; $obj_self->{aaa} = 'ZZZ'; print "第ï¼å¼æ°ã¯ $obj_self ã§ãªãã¸ã§ã¯ãã«ãªãã¾ãã\n" } print "\n", 'âãã¤ã³ãï¼ã®ä¾ ãªãã¸ã§ã¯ãä¾ï¼å§è²ï¼---', "\n"; my $obj_package02 = package02->new(); # newã¯ãªãã¸ã§ã¯ãã³ã³ã¹ãã©ã¯ã¿ã§ãããèµ·åï¼å§è²ï¼ãã # $obj_package02ã«ã¯ã¤ã³ã¹ã¿ã³ã¹ãæ ¼ç´ããã print '-->', $obj_package02->{bbb}, '<--', "\n"; $obj_package02->test02(4); # ãªãã¸ã§ã¯ãã®ã¡ã½ãããèµ·åãã print '-->', $obj_package02->{bbb}, '<--', "\n"; package package02; # ããã±ã¼ã¸ã¯ãPerlã§ã¯ã¯ã©ã¹ã«ãªã sub new { print "第ï¼å¼æ°ã¯$_[0]ã§ã¯ã©ã¹åã§ãã", "\n"; my $class = shift; my %test_HASH = (aaa => 'XXX', bbb =>'YYY'); return bless \%test_HASH, $class; } sub test02 { print "第ï¼å¼æ°ã¯$_[0], 第ï¼å¼æ°ã¯$_[1]ã§ãã", "\n"; my $obj_self = shift; $obj_self->{bbb} = 'ZZZ'; print "第ï¼å¼æ°ã¯ $obj_self ã§ãªãã¸ã§ã¯ãã«ãªãã¾ãã\n" }
â ãªãã¸ã§ã¯ãæåã«ã¤ãã¦
Perlã¯å
ã
ã¯ããªãã¸ã§ã¯ãæåè¨èªã§ã¯ããã¾ããã
ããããæè»ãªè¨èªä»æ§ã®Perlã§ã¯ããªãã¸ã§ã¯ãæåè¨èªã®ããã«æ¯ãèããã¨ãå¯è½ãªã®ã§ãã
æ®éããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã§ã¯ã以ä¸ã®è¦ç´ ãå¿ è¦ã§ãã
- ã¯ã©ã¹ï¼Perlã§ã¯ããã±ã¼ã¸ã§å¯¾å¿ï¼
- ã¤ã³ã¿ã¼ãã§ã¼ã¹ï¼Perlã§ã¯ä¸å®å ¨ã ãã©ããã±ã¼ã¸ã§å¯¾å¿ï¼
- ã³ã³ã¹ãã©ã¯ã¿ï¼Perlã§ã¯blessãªãã¡ã¬ã³ã¹ã§å¯¾å¿ï¼
- ãªã¼ãã¼ã©ã¤ãï¼Perlã§ã¯æ®éã«å¯¾å¿ï¼
- ãªã¼ãã¼ãã¼ãï¼Perlã§ã¯è¤æ°ã®ååsubã¯ç¡çãããããä¸ã¤ã®subã§ãªã¼ãã¼ãã¼ããã©ãã¯å¯è½ï¼
- ã«ãã»ã«åï¼Perlã§ã¯æ¨æºè¨èªä»æ§çã«ã¯é£ãããããããAttribute::Protectedã¢ã¸ã¥ã¼ã«çã§å¯è½ï¼
- ç¶æ¿ï¼Perlã§ã¯baseãã©ã°ãã§å¯¾å¿ï¼
- å§è²ï¼Perlã§ã¯æ®éã«å¯¾å¿ï¼
- å¤æ æ§ï¼Perlã§ã¯æ®éã«å¯¾å¿ï¼
ã¨ãããã¨ã§ãã«ãã»ã«åã¨ãªã¼ãã¼ãã¼ã以å¤ã¯ã大ä¸å¤«ãªã®ã§ãã
以ä¸ã«ãã¯ã©ã¹ãã³ã³ã¹ãã©ã¯ã¿ãç¶æ¿ãå¤æ
æ§ããªã¼ãã¼ã©ã¤ãã使ç¨ãããµã³ãã«ãæ¸ãã¦ã¿ã¾ããã
âãµã³ãã«
use strict; use warnings; my $obj; my $hiki = shift; if ($hiki eq '1') { $obj = Otokonoko->new(); } else { $obj = Onnanoko->new(); } print $obj->getName(), "\n"; $obj->shumi(); package Person; sub new { my $class = shift; my $self = { Name => $class, }; return bless $self, $class; } sub getName {}; package Otokonoko; use base 'Person'; sub getName { my $self = shift; return $self->{Name}; } sub shumi { print "ãã¹ã±\n"; } package Onnanoko; use base 'Person'; sub getName { my $self = shift; return $self->{Name}; } sub shumi { print "ã±ã¼ãä½ã\n"; }
âå®è¡çµæ
C:\perltest>perl tatai.pl 1 Otokonoko ãã¹ã± C:\perltest>perl tatai.pl 2 Onnanoko ã±ã¼ãä½ã
- ã¯ã©ã¹ã¯ãPersonãOtokonokoãOnnanokoã®ããã±ã¼ã¸ã使ç¨ããã
- ã³ã³ã¹ãã©ã¯ã¿ã¯ããreturn bless $self, $class;ãã§blessãªãã¡ã¬ã³ã¹ãã³ã³ã¹ãã©ã¯ãããã
- ç¶æ¿ã¯ããuse base 'Person';ãã§baseãã©ã°ãã使ç¨ããã
- å¤æ æ§ã¯ãåçãªæ¡ä»¶ã«ãã£ã¦ãäºãã³ã¼ãã£ã³ã°ãã¦ãããªãã¸ã§ã¯ãã®ã¡ã½ããã®æ¯ãèããæ¿ããã
- ãªã¼ãã¼ã©ã¤ãã¯ãã¹ã¼ãã¼ã¯ã©ã¹ã§ã®æ½è±¡getNameã¡ã½ããããµãã¯ã©ã¹ã®å ·è±¡getNameã¡ã½ããã§ç½®ãæããã
â ãã¶ãç¸äºãªã³ã¯
- (基礎|変態)文法最速マスターシリーズのまとめ - gifnksmの雑多なメモ
- プログラミング基礎文法最速マスターまとめ - ネットサービス研究室
- はてな的プログラミング言語人気ランキング - Life like a clown
- きまぐれメモ
- 基礎文法最速マスター - おなかすいたWiki!
ã§ããã°ãæ¬ããã°ã®å¿æ´ã¯ãªã㯠ãããããããé¡ãè´ãã¾ãã