ã¯ãã¼ã¸ã£ã¼ãä½æããæ¹æ³
Perl
›
ãµãã«ã¼ãã³
›
here
Perlã§ãã¯ãã¼ã¸ã£ã¼ããä½æããæ¹æ³ã解説ãã¾ãã
ã¯ãã¼ã¸ã£ã®å®ç¾©
ã¯ãã¼ã¸ã£ã®å®ç¾©ãã³ã¼ãã使ã£ã¦è§£èª¬ãã¾ãã
use strict; use warnings; # ãã£ã¨ãã·ã³ãã«ãªã¯ãã¼ã¸ã£ { # å¤æ°ã®çæ my $var = 5; sub var { return $var; } } # ã¹ã³ã¼ããçµãã£ã¦ãvarãµãã«ã¼ãã³ã«åç §ããã¦ããããã # $var ã¯åå¨ãç¶ããã # var ãµãã«ã¼ãã³ã ããã$var ãè¦ããã¨ãå¯è½ã print "1: ãã£ã¨ãã·ã³ãã«ãªã¯ãã¼ã¸ã£\n"; print var(), "\n";
ã¯ãã¼ã¸ã£ã®å®ç¾©
ã¯ãã¼ã¸ã£ã§ããããã®æ¡ä»¶ã¯ããµãã«ã¼ãã³ããèªåã®ã¹ã³ã¼ãå¤ã®ã¬ãã·ã«ã«å¤æ°ãåç §ãã¦ãããã¨ã§ãã( ãã®ä¾ã§ã¯ãvar ããèªåã®ã¹ã³ã¼ãå¤ã®$var ãåç §ã)
ã¯ãã¼ã¸ã£ã®å®ç¾©ã¯äººãç¶æ³ã«ãã£ã¦ããã¾ãã§ããvarãµãã«ã¼ãã³ã®ãã¨ãã¯ãã¼ã¸ã£ã¨å¼ã¶ãã¨ãããã¾ããããvarãµãã«ã¼ãã³ã¨åç §ããã¦ããå¤æ°$var ãå«ãç°å¢ãã®ãã¨ãã¯ãã¼ã¸ã£ã¨å¼ã¶ãã¨ãããã¾ãã
ãµãã«ã¼ãã³ãä½æããã¨ãã¯ãå¿ ãå¼æ°ã¨ãã¦å¤æ°ãåãåã£ã¦ãã¹ã³ã¼ãå¤ã®å¤æ°ãåç §ãã¦ã¯ãããªãã¨æ¸ãã¾ããããã¯ãã¼ã¸ã£ã¯ä¾å¤ã®ã²ã¨ã¤ã§ãã
Perlã®ã¹ã³ã¼ãã«ã¤ãã¦ã¯ä»¥ä¸ã®è¨äºãåèã«ãã¦ãã ããã
ã¯ãã¼ã¸ã£ã®ç¹å¾´
{ # å¤æ°ã®çæ my $var = 5; sub var { return $var; } }
$var ã¯ãvarãµãã«ã¼ãã³å ã§åç §ããã¦ããããã«ãã¹ã³ã¼ããçµäºãã¦ããã¡ã¢ãªä¸ããåé¤ãããªãã( ã¬ãã·ã«ã«å¤æ°ã¯ãã¹ã³ã¼ããçµãã£ã¦ãã©ããããåç §ããã¦ããªãã¨ãã«ã¡ã¢ãªä¸ããç ´æ£ããããï¼
ã¹ã³ã¼ããçµäºããã¨ãã¹ã³ã¼ãå¤ããã¯ã$var ãè¦ããªããªãã( varãµãã«ã¼ãã³ã¯ãã¹ã³ã¼ãã®å½±é¿ãåããªããããã¹ã³ã¼ãå¤ããã¯è¦ããã)
$var ãè¦ããã¨ãã§ããã®ã¯ãvar ãµãã«ã¼ãã³ã ãã§ãããã¹ã³ã¼ããçµäºããå¾ã¯ãvar ãµãã«ã¼ãã³ãéãã¦ã®ã¿ã $var ã®å¤ã«ã¢ã¯ã»ã¹ãããã¨ãã§ããã
ã¯ãã¼ã¸ã£ã®ã¤ã¡ã¼ã¸
|-------------ã¹ã³ã¼ã( ã¯ãã¼ã¸ã£ )--------------| | | | |--------| |--------------| | |------------| | | | ã¢ã¯ã»ã¹å¯è½ | | ã¢ã¯ã»ã¹å¯è½ | | | | ãã¼ã¿ |<-------------| ãµãã«ã¼ãã³ |<-------------| ã¹ã³ã¼ãå¤ | | | | | | | | | | |--------| |--------------| | |------------| | | |-------------------------------------------------| ã¹ã³ã¼ãå¤ãããã¼ã¿ã«ã¯ã¢ã¯ã»ã¹ä¸è½
ã¹ã³ã¼ãå¤ããã¯ããµãã«ã¼ãã³ãéãã¦ã®ã¿ãã¼ã¿ã«ã¢ã¯ã»ã¹å¯è½ãç´æ¥ãã¼ã¿ã«ã¢ã¯ã»ã¹ãããã¨ãã§ããªãã
ã¯ãã¼ã¸ã£ããé¢æ°ã¸ã§ãã¬ã¼ã¿ãã使ã£ã¦ä½æãã
ã¯ãã¼ã¸ã£ãé¢æ°ã¸ã§ãã¬ã¼ã¿ã使ã£ã¦ä½æãã¦ã¿ã¾ãã
use strict; use warnings; # ã¿ã¤ãã¼ãçæããã print "1: é¢æ°ã¸ã§ãã¬ã¼ã¿ã§ã¯ãã¼ã¸ã£ãçæãã( ã¿ã¤ãã¼ )ã\n"; my $timer1 = create_timer(); sleep 1; # 1ç§å¾ã«ããã²ã¨ã¤ã¿ã¤ãã¼ãçæ my $timer2 = create_timer(); # 1ç§æ¢ã¾ã sleep 1; print '$timer1 ã«ããçµéç§: '; print $timer1->(), "\n"; print '$timer2 ã«ããçµéç§: '; print $timer2->(), "\n\n"; # çµéæå»ãç¥ãã¯ãã¼ã¸ã£ãçæããé¢æ°ã¸ã§ãã¬ã¼ã¿ sub create_timer { # time() ã¯ãé¢æ°ã¸ã§ãã¬ã¼ã¿ãå¼ã³åºãããæå» my $time = time(); return sub { # time() ã¯çæããããµãã«ã¼ãã³ã # å¼ã³åºãããæå»ãtime() - $time 㧠# çµéæå»ãç¥ããã¨ãã§ããã return time() - $time; } }
ã¯ãã¼ã¸ã£ãçæããããã®é¢æ°ã¸ã§ãã¬ã¼ã¿
sub create_timer { # time() ã¯ãé¢æ°ã¸ã§ãã¬ã¼ã¿ãå¼ã³åºãããæå» my $time = time(); # time() ã¯çæããããµãã«ã¼ãã³ã # å¼ã³åºãããæå»ãtime() - $time 㧠# çµéæå»ãç¥ããã¨ãã§ããã return sub { return time() - $time; } }
é¢æ°ã¸ã§ãã¬ã¼ã¿ã¨ã¯ããµãã¼ãã³ãçæãããµãã«ã¼ãã³ã®ãã¨ã§ããé¢æ°ã¸ã§ãã¬ã¼ã¿ãå©ç¨ããã°ãè¤æ°ã®ã¯ãã¼ã¸ã£ãçæãããã¨ãã§ãã¾ãã
çæãããããããã®ã¯ãã¼ã¸ã£ã¯ããã¼ã¿ãç¬èªã«æã¡ã¾ãããã®ä¾ã§ã¯ã $time ã®å¤ã¯ãã¡ã¢ãªä¸ã«ã¯ãã¼ã¸ã£æ¯ã«ä½æããã¾ããæ»ãå¤ã¯ãµãã«ã¼ãã³ã¸ã®ãªãã¡ã¬ã³ã¹ã«ãªã£ã¦ãã¾ããããã¯ãPerlã§ã¯ããµãã«ã¼ãã³ãã®ãã®ãæ»ãå¤ã«ãããã¨ãã§ããªãããã§ãã
é¢æ°ã¸ã§ãã¬ã¼ã¿ã®å¼ã³åºãã¦ã¯ãã¼ã¸ã£ãä½æãã
my $timer1 = create_timer(); my $timer2 = create_timer();
æ»ãå¤ã¯ããµãã«ã¼ãã³ã¸ã®ãªãã¡ã¬ã³ã¹ã«ãªãã¾ãããã®ãµãã«ã¼ãã³ã¯ã¯ãã¼ã¸ã£ã§,ç¬èªã«ã¡ã¢ãªä¸ã«ç°å¢( ãµãã«ã¼ãã³ã«åç §ãããå¤æ°ã¨ãµãã«ã¼ãã³ã®å®ç¾© )ãæã¡ã¾ãã
ã¯ãã¼ã¸ã£ãå¼ã³åºã
$timer1->();
ãµãã«ã¼ãã³ã¸ã®ãªãã¡ã¬ã³ã¹ãããã£ãã®ã§ãããªãã¡ã¬ã³ã¹ãã¦å¼ã³åºãã¾ãã
ã¯ãã¼ã¸ã£ã®å©ç¨æ¹æ³
ã¯ãã¼ã¸ã£ãå©ç¨ããã¨ããåºæºã«ãªãæç¹ã§ã®ç¶æ ãã¨ãç¾æç¹ã§ã®ç¶æ ãã®å¤åãç°¡æ½ã«è¨è¿°ãããã¨ãã§ãã¾ãã
ã¯ã©ã¹ãä½ãã°åããã¨ãå®ç¾ã§ãã¾ãããã¯ã©ã¹ãä½ããããªãå ´åã¯ãã¯ãã¼ã¸ã£ãå©ç¨ãã¾ãã( ã¯ã©ã¹ãã²ã¨ã¤ä½ãã°ãã¯ã©ã¹ã管çããæéãå¢ãã¾ãããã®ä¾ã®ããã«ã·ã³ãã«ãªè¨è¿°ã®ããã«ããããã¯ã©ã¹ãä½ããããªãå ´åã«ãã¯ãã¼ã¸ã£ã使ãã¾ãã )
ã¯ãã¼ã¸ã£ã«åç §ããããå¤æ°ãåæåããã
ã¯ãã¼ã¸ã£ã«åç §ãããå¤æ°ãåæåãããµã³ãã«ã§ãã
use strict; use warnings; # é¢æ°ã¸ã§ãã¬ã¼ã¿ã«å¼æ°ã¨ãã¦æ¸¡ãã print "1: ã¯ãã¼ã¸ã£ã«åç §ãããå¤æ°ãåæåããã\n"; my $sign_inversion = create_sign_checker(1); if ($sign_inversion->(-2)) { # 符å·ãå転ãã¦ããã print "-2 ã¯ã1ããè¦ã¦ç¬¦å·ãå転ãã¦ãã¾ãã\n"; } # 符å·ãå転ãããã©ããã確ãããã¯ãã¼ã¸ã£ãçæããé¢æ°ã¸ã§ãã¬ã¼ã¿ sub create_sign_checker { # ã¯ãã¼ã¸ã£ãä½æããã¨ãã«åæåãããã my $num1 = shift; return sub { # ã¯ãã¼ã¸ã£ãå¼ã³åºããã¨ãã®å¼æ°ã my $num2 = shift; if ($num1 * $num2 > 0) { # å転ãã¦ããªãå ´å return 0; } elsif ($num1 * $num2 < 0) { # å転ãã¦ããå ´å return 1; } else { # ã©ã¡ããã0ã®å ´åundefãè¿ã return; } } }
ã¯ãã¼ã¸ã£ã«åç §ãããå¤æ°ãåæåãã
# ã¯ãã¼ã¸ã£ãçæã( ã¯ãã¼ã¸ã£ã«åç §ãããå¤æ°ã1ã§åæå ) my $sign_inversion = create_sign_checker(1); # ã¯ãã¼ã¸ã£ãå¼ã³åºã $sign_inversion->(-2); # ã¯ãã¼ã¸ã£ãçæããé¢æ°ã¸ã§ãã¬ã¼ã¿ sub create_sign_checker { # ã¯ãã¼ã¸ã£ãä½æããã¨ãã«åæåãããã my $num1 = shift; # ã¯ãã¼ã¸ã£ã®å®ç¾© return sub{ # ã¯ãã¼ã¸ã£ãå¼ã³åºããã¨ãã®å¼æ°ã my $num2 = shift; # ... } }
ã¯ãã¼ã¸ã£ãä½æããã¨ãã«ãã¯ãã¼ã¸ã£ãåç §ããå¤æ°ãåæåãããã¨ãã§ãã¾ããã¯ãã¼ã¸ã£ãä½æããã¨ãã«å¼æ°ã¨ãã¦æ¸¡ãã¾ããããããé¢æ°ã¸ã§ãã¬ã¼ã¿ã®å¼æ°ã¨ãã¦åãåã£ã¦ãå¤æ°ã«è¨å®ãã¾ãã
ã¯ãã¼ã¸ã£ã®æ¦å¿µãã¯ã©ã¹ã¨ã®å¯¾æ¯ã§ãããããã説æãã
ã¯ãã¼ã¸ã£ã®æ¦å¿µã¯ã¼ãã£ã¨ãã¦ãã¦è¼ªéãæãããã®ãé£ããã§ããããããªãã«ã§ããã ãç解ã§ããããã«è§£èª¬ãããã¨æãã¾ãã
ã¯ãã¼ã¸ã£ã¨ã¯
- ã¯ãã¼ã¸ã£ã¼ã¨ã¯ããã¼ã¿ã¨ãã®ãã¼ã¿ãæä½ããé¢æ°ã®ã»ããã§ããã
- ãã¼ã¿ã¯ã«ãã»ã«åããã¦ãã¦ããã¼ã¿ãæä½ã§ããã®ã¯ãã¯ãã¼ã¸ã£å é¨ã®é¢æ°ã ãã§ããã
- ãã¼ã¿ã®åæåãå¯è½ã§ããã
- ãã¼ã¿ãã¨ã«åã ã®ã¤ã³ã¹ã¿ã³ã¹ãä½æãããã¨ãã§ããã
- é¢æ°ã¯ã¤ã³ã¹ã¿ã³ã¹ããå¼ã³åºãããã
次ã«ããªãã¸ã§ã¯ãæåã«ãããã¯ã©ã¹ã«ã¤ãã¦ã
ã¯ã©ã¹ã¨ã¯
- ã¯ã©ã¹ã¨ã¯ããã¼ã¿ã¨ãã®ãã¼ã¿ãæä½ããé¢æ°ã®ã»ããã§ããã
- ãã¼ã¿ã¯ã«ãã»ã«åããã¦ãã¦ããã¼ã¿ãæä½ã§ããã®ã¯ãã¯ã©ã¹å é¨ã®é¢æ°ã ãã§ããã
- ãã¼ã¿ã®åæåãå¯è½ã§ããã
- ãã¼ã¿ãã¨ã«åã ã®ã¤ã³ã¹ã¿ã³ã¹ãä½æãããã¨ãã§ããã
- é¢æ°ã¯ã¤ã³ã¹ã¿ã³ã¹ããå¼ã³åºãããã
ããã§æ°ã¥ãã¦ã»ãããã¨ã¯ãã¯ã©ã¹ã¨ã¯ãã¼ã¸ã£ã¯ãå®ã¯åããã®ã ã¨ãããã¨ã§ããPerlã®å®è£ ã«ããã¦ã¯ãã¯ã©ã¹ã¨ã¯ãã¼ã¸ã£ã¯ãææ³çãªæ¸ãæ¹ã«ããã¦éããããã¾ããããã©ãæ©è½çã«ã¯åããã®ã§ããã¯ãã¼ã¸ã£ã¨ã¯ç°¡æãªã¯ã©ã¹ã®ãã¨ã ã¨æãã°ç解ããããã§ãã(ã¯ãã¼ã¸ã£ã®æ¦å¿µã¯ãããå°ãåºãããã©ãæåã®ãã¡ã¯ç°¡æãªã¯ã©ã¹ã ã¨ç解ããã)
ã¯ã©ã¹ã¨ã¯ãã¼ã¸ã£ã®ç¸éç¹
- ã¯ãã¼ã¸ã£ã¯ãã¯ã©ã¹åãæããªããmy $ins = Class->new; ã¨ããnewãå¼ã³åºããã¨ã§ãã¤ã³ã¹ã¿ã³ã¹ãçæããã®ã§ã¯ãªãã my $ins = closure_maker(); ã¨ããå½¢ã§ã¤ã³ã¹ã¿ã³ã¹ãçæããã
- ã¯ãã¼ã¸ã£ã¯ãç¶æ¿ã§ããªããã¯ã©ã¹ã®ããã«ç¶æ¿ããããã¨ã¯ã§ãã¾ããã
ã¯ã©ã¹ã¨ã¯ãã¼ã¸ã£ã使ã£ã¦ãæ°ãæ°ããããã®æ©è½(ã«ã¦ã³ã¿ã¼)ãå®è£ ãã¦ã¿ã¾ãã
ã«ã¦ã³ã¿ã¼ãã¯ã©ã¹ã§å®è£ ãã
use strict; use warnings; package Counter; sub new{ my $class = shift; my $self = {}; $self->{cnt} = shift; bless $self, $class; } sub add_count{ my $self = shift; $self->{cnt}++; } sub count{ my $self = shift; return $self->{cnt}; } package main; my $cnt = Counter->new(10); print "1: ã¯ã©ã¹ã«ããã«ã¦ã³ã¿ã¼ã®å®è£ \n"; print "ã«ã¦ã³ãå:", $cnt->count, "\n"; $cnt->add_count; print "ã«ã¦ã³ãå¾:", $cnt->count, "\n\n";
ã«ã¦ã³ã¿ã¼ãã¯ãã¼ã¸ã£ã§å®è£ ãã
use strict; use warnings; # ã¯ãã¼ã¸ã£ sub make_counter{ my $self = {}; $self->{cnt} = shift; # ã¯ãã¼ã¸ã£ã§ã¯ããµãã«ã¼ãã³ã¸ã®ãªãã¡ã¬ã³ã¹ã使ã£ã¦ã¡ã½ãããå®è£ ããã my $funcs = {}; $funcs->{add_count} = sub { $self->{cnt}++; }; $funcs->{count} = sub { return $self->{cnt}; }; return $funcs; } my $cnt = make_counter(10); print "2: ã¯ãã¼ã¸ã£ã«ããã«ã¦ã³ã¿ã¼ã®å®è£ \n"; print "ã«ã¦ã³ãå:" . $cnt->{count}->() . "\n"; $cnt->{add_count}->(); print "ã«ã¦ã³ãå¾:" . $cnt->{count}->() . "\n";
ãã®ããã«ã¯ã©ã¹ã¨ã¯ãã¼ã¸ã£ã¯ãããä¼¼ã¦ãã¾ãã次åã¯ãã³ã¼ãã解説ãã¾ãã
ã¯ã©ã¹ã¨ã¯ãã¼ã¸ã£ã®è¦è¦çãªã¤ã¡ã¼ã¸
## ã¯ã©ã¹ |------ã¯ã©ã¹------| | ã»ãã¼ã¿ | | ã»ãã¼ã¿æä½é¢æ° | |------------------| ## ã¯ãã¼ã¸ã£ |----ã¯ãã¼ã¸ã£----| | ã»ãã¼ã¿ | | ã»ãã¼ã¿æä½é¢æ° | |------------------|
å°é£¼ å¼¾ããã®æ·»å
- perl - Class vs. Closure(404 Blog Not Found)
- ã¯ãã¼ã¸ã£ã§ã¯ã©ã¹ã¨åããã¨ãã§ããããã©ãPerlã§ã¯ã¯ãã¼ã¸ã£ã®çæã¯ããªãã¸ã§ã¯ãã®çæãããã³ã¹ãããããããã§ãã
- ã¯ãã¼ã¸ã£ã¯ãç¶æ¿ã§ããªãã¨æ¸ãã¾ãããç¶æ¿ã§ããã¿ããã§ãã(ããããã¯ä»ã®ã¨ããã¯æãã¤ãã¾ããã)
Perl
›
ãµãã«ã¼ãã³
›
here