ãã®è¨äºã¯Perl Advent Calendar 2024 17æ¥ç®ã®è¨äºã§ããæ¨æ¥ã¯karupaneruraããã®String::Secretã®ãç´¹ä»ã§ãããæ¼ç®åãªã¼ãã¼ãã¼ãã§æååã§ãªããã®ãæååã«è¦ããããã®ã¯åããã£ããã¨ãããã¾ããã
ãã¦ãargathãããAdvent Calendarã®13æ¥ç®ã«æ¬¡ã®Perlã¯Perl 42(ãã)ã¨ããè¨äºãæ¸ãã¦ãã¾ããããã®è¨äºãããªã³ã¯ããã¦ããPerl 5 is Perlã¨ããè¨äºãèªãã§ã¿ãã¨ã
30å¹´åã®1994å¹´10æ17æ¥ã«5.0ã«ãªãã¾ããã
ã¨ããã¾ããã¤ã¾ããç¾å¨åºã使ããã¦ããPerl 5ã¯ãªãªã¼ã¹ãã30å¹´ãéããã¨ãããã¨ã«ãªãã¾ããããã¯ã¨ãã§ããªããã¨ã§ããããã®è¨äºã«ã¯ãããããé²åãç¶ãã¦ããæ¨ãæ¸ããã¦ãã¾ãããã¨ããPerlã使ããªãæ¹ããèãããç¹ã¨ãã¦ãPerl 6ã¯ãã¤ãªãªã¼ã¹/移è¡ãããã®ãã¨ãã話ã§ãããPerl 6ã¯æ¢ã«ãªãªã¼ã¹ãããä¸ã§ãRakuã¨ããååã«å¤ãã£ã¦ãã¾ãããªãªã¼ã¹ãããæç¹ã§ããã§ãããPerl 5ã®å¾ç¶ã¯Perl 6ã§ã¯ãªããå§å¦¹çãªè¨èªã§ããã¨ããã¦ãã¾ãã
ã¨ããã§ãææ°ã®ãã¼ã¸ã§ã³ã§ãã5.40ãããããåã®5.38,5.36,5.34ã«ã¯å¤ãã®æ©è½ã追å ããã¦ãã¾ããç®ç«ã£ããã®ã§è¨ãã¨ä»¥ä¸ã®ãããªæãã§ãã
- try/catch/finally
- class
- true/false
- for my ($first, $second) (@list)
使ããªããªã£ããã®ã®ä¾: éæ¥ãªãã¸ã§ã¯ãè¨æ³
ä¸æ¹ã§ã使ããªããªã£ããã®ãããã¾ãã代表ä¾ã¨ãã¦ã¯ãéæ¥ãªãã¸ã§ã¯ãè¨æ³ãããã¾ããå³å¯ã«ã¯ä½¿ããªãããã§ã¯ãªããææ°ã®Perlã®æ©è½ãæå¹ã«ãã use v5.40;
ãã¹ã¯ãªããå
ã«è¨è¿°ããã¨ä½¿ããªããªãã¾ããã©ããããã¨ãã
ã¾ãéæ¥ãªãã¸ã§ã¯ãè¨æ³ããããããã¾ãã以ä¸ã®ã³ã¼ãã§ã¯ãFooã¯ã©ã¹ãå®ç¾©ããã¤ã³ã¹ã¿ã³ã¹ãä½æããnewã¡ã½ããã¨ãhelloã¡ã½ãããå®ç¾©ãã¦ãã¾ããããã¦ããããå¼ã³åºãã¦ãã¾ãã
éæ¥ãªãã¸ã§ã¯ãè¨æ³ã¨ã¯ä»¥ä¸ã®ã³ã¼ãä¸ã§ã¯ new Foo
㨠hello Foo
ã«ãããã¾ãã
use strict; use warnings; use utf8; package Foo { sub new { my $class = shift; my %obj = (name => "Fooooo"); bless \%obj, $class; } sub hello { my $self = shift; print $self->{name} . ": hello\n"; } } my $foo = new Foo; hello $foo;
ã¨ããã§ãåºã使ãããPerlã¯ãã®ããã«ã¯ã©ã¹ã®ã¡ã½ããå¼ã³åºããæ±ãã¾ãããä¸è¬çã«ã¯ããã§ãã
my $foo = Foo->new; $foo->hello;
ã¤ã¾ãéæ¥ãªãã¸ã§ã¯ãè¨æ³ã¯ã¢ãã¼æ¼ç®å->
ã§ã¯ãªããJavaãªã©ã«è¿ãè¨æ³ã§ã¡ã½ããå¼ã³åºããè¡ãç³è¡£æ§æã§ããã¨è¨ãã¾ãã
ä¸æ¹ã§ãuse v5.40;
ãè¡ãã¨ä¸è¨ã®ã³ã¼ãã¯ã©ããªãã§ãããããstrict
ã¨warnings
ãã©ã°ãã®ä»£ããã«v5.40
ãã©ã°ããå
¥ãã¾ãã(ãªããªãv5.40
ã«ããã2ã¤ã®ãã©ã°ãã¯å«ã¾ããããã§ã)
use v5.40; use utf8; package Foo { sub new { my $class = shift; my %obj = (name => "Fooooo"); bless \%obj, $class; } sub hello { my $self = shift; print $self->{name} . ": hello\n"; } } my $foo = new Foo; hello $foo;
ãã®ã³ã¼ãã¯åãã¾ããã以ä¸ã«ã¨ã©ã¼ã示ãã¾ãã
Bareword found where operator expected (Do you need to predeclare "new"?) at prog.pl line 16, near "new Foo" syntax error at prog.pl line 16, near "new Foo" Execution of prog.pl aborted due to compilation errors.
new
ã¨ããé¢æ°ããªãã¨èµ·ãããã¦ãã¾ãããã®æ¸ãæ¹ã§new
ãFoo
ã®ã¡ã½ããã§ããã¨èªèãããã¨ã¯ãªããªããåãªãé¢æ°å¼ã³åºãã¨ãã¦è§£éãããã¨ãã¦ãããã¨ããããã¾ãã
ã§ã¯ãªããéæ¥ãªãã¸ã§ã¯ãè¨æ³ã¯ãªããªã£ã¦ãã¾ã£ãã®ã§ããããï¼ Perldocã®éæ¥ãªãã¸ã§ã¯ãè¨æ³ã®èª¬æã«ãã£ããã¨æ¸ããã¦ãã¾ãã
ã¤ã¾ããä¸è¨ã®ã³ã¼ãã ã¨ãnew
ãhello
ãé¢æ°ãªã®ããããã¨ãã¡ã½ãããªã®ããåºå¥ããã®ãèªãå´ã«ãé£ãããã¾ãã¤ã³ã¿ããªã¿ã«å¯¾ãã¦ãééã£ãæ示ãä¸ãã¦ãã¾ããã¨ãããã¾ãã
Try::Tiny
ã使ããã¨ãããåãã®ã«ãæåãæå³éãã«ãªããªãã¨ããä¾ãããã¾ãã
ãããªãããªã§ãéæ¥ãªãã¸ã§ã¯ãè¨æ³ã¯å½æã¯æ¸ãæ¹ããã£ãããã£ããã®ã®(ç¹ã«newã¡ã½ãã)ãä»ã¨ãªã£ã¦ã¯éæ¨å¥¨ã«ãªããããã«use v5.36;
ãããã¨ç¡å¹åãããã¨ãããã¨ã«ãªãã¾ããã
Perlã¯å®ã¯éå»ã«å®è£ ããã¦åºã使ããã¦ãããã®ã®ãééããèµ·ããããããªã©ã®ãããªãææ³ãç¡å¹åãããããã«ãªã£ã¦ãã¦ãããã§ããããã§ãããã§ããã
å¾æ¹äºææ§ãå®ãä»çµã¿
ããã ã¨ä»ã®è¨èªã§ããããããã¨ã§ãããï¼ããããå¾æ¹äºææ§ã鬼å®ãPerlã§ã¯å°ãéãã¾ãã
ãã£ãè¨ã£ãããã«ææ°ã®ã¤ã³ã¿ããªã¿ã§ããperl 5.40ã§ãã£ã¦ãuse v5.36;
以ä¸ãããªããã°éæ¥ãªãã¸ã§ã¯ãè¨æ³ã¯ä½¿ç¨å¯è½ã§ããå³å¯ã«ã¯ãuse feature 'indirect';
ã¨ããã¨ä½¿ç¨å¯è½ã§ãã
ããã¦ãéæ¥ãªãã¸ã§ã¯ãè¨æ³ã使ç¨ãã¦ããã³ã¼ãã¯ç¡è¦ã§ããªãç¨åº¦ã«ã¯åå¨ããã¨æããã¾ãããããæ°ããè¨æ³ã使ãããããããããã¯èªåãééãã¦ãã¾ãã®ã§éæ¥ãªãã¸ã§ã¯ãè¨æ³ãç¡å¹åããã¨æ¢åã³ã¼ããå£ãã¦ãã¾ãã¨ãããã¨ã«ãªã£ã¦ãã¾ãã¨æãã¾ãããï¼
ããããPerlã®æ©è½ã¹ã¤ãããè¡ãuse VERSION;
ã¯ã¹ã³ã¼ãã«å¹ãã¨ããç¹å¾´ãããã¾ãããã¡ã¤ã«ã§ã¯ããã¾ãããã¹ã³ã¼ãã«å¹ãã¾ãã以ä¸ã®ã³ã¼ããè¦ã¦ã¿ã¾ãããã
use strict; use warnings; use utf8; package Foo { sub new { my $class = shift; my %obj = (name => "Fooooo"); bless \%obj, $class; } sub hello { my $self = shift; print $self->{name} . ": hello\n"; } } my $foo = new Foo; hello $foo; { use v5.40; my $foo2 = new Foo; hello $foo2; } my $foo3 = new Foo; hello $foo3;
Perlã¯{ ... }
ã§ãããã¯ã¹ã³ã¼ããä½ãã¾ãã以ä¸ã®ã³ã¼ãã§ã¯ãããã¯ã¹ã³ã¼ãå
ã§use v5.40;
ã宣è¨ãã¦ãã¾ãããã®ã³ã¼ãã¯ä»¥ä¸ã®ã¨ã©ã¼ãåºã¾ãã
Bareword found where operator expected (Do you need to predeclare "new"?) at prog.pl line 22, near "new Foo" syntax error at prog.pl line 22, near "new Foo" Execution of prog.pl aborted due to compilation errors.
ã¨ã©ã¼ã§ææããã¦ãã22è¡ç®ã¯my $foo2 = new Foo;
ã«ãããã¾ãããã£ã¨æåã®my $foo = new Foo;
ã§ã¯ã¨ã©ã¼ãåºã¦ãã¾ãããããã¦ã22è¡ç®ãmy $foo2 = Foo->new;
23è¡ç®ã $foo2->hello;
ã«å¤ããã¨æ£å¸¸ã«åä½ãã¾ããuse v5.40;
ã®åã¶ç¯å²ããããã¯ã¹ã³ã¼ãã«éå®ããã¦ãããã¨ããããã¾ãã
ã¾ãããã®ã¹ã³ã¼ãã¨ã¯ã¬ãã·ã«ã«ã¹ã³ã¼ãã§ããã©ããããã¨ãã¨ããã¨ãuse v5.40;
ã¨å®£è¨ããã¹ã³ã¼ããã宣è¨ãã¦ããªãã¹ã³ã¼ãã®é¢æ°ãå¼ã³åºããã¨ãã¦ããå¼ã³åºãå
ã®ã¹ã³ã¼ãã®use v5.40;
ã¯å¼ã³åºãå
ã®ã¹ã³ã¼ãã«ã¯é©ç¨ããã¾ããã
ã¤ã¾ã以ä¸ã®ã³ã¼ãã¯åãã¾ãã
sub hello_foo { my $foo = new Foo; hello $foo; } { use v5.40; hello_foo; }
ãã®ãã¨ãããèªåãæ°ãã«ãããç¯å²ã®ã¿ãuse v5.40;
ã¨ãããã¨ã§ãéæ¥ãªãã¸ã§ã¯ãè¨æ³ã®ããã©ããããã解æ¾ãããã¾ãæ¢åã®ã³ã¼ãã«ã¯æãå
¥ããã«ä¿®æ£ãããã¨ãå¯è½ã«ãªãã¾ãã
ä¸æ¹ã§ããã¦ã³ã°ã¬ã¼ãã«ã¯ç¾å¨ä¸é¨ã®ãã¼ã¸ã§ã³ã®çµã¿åããã§ã¯ã¨ã©ã¼ã«ãªãããã®ä»ã®çµã¿åããã§ã¯è¦åãåºãããã«ãªã£ã¦ãã¾ãããã¦ã³ã°ã¬ã¼ãã¯å¾æ¹äºææ§ã®è¦³ç¹ã§ã¯ã¾ã使ããªãã¨ã¯æãã¾ãããã注æãã ããã
ãããã§ããã§ããããããã®ã»ãã«ãuse VERSION;
ã¯è¨èªæ©è½ã®ã¹ã¤ããããã¼ã¸ã§ã³ã§åçã«ç®¡çãããªã©ãé¨åé¨åã§æ°ããæ©è½ã使ããããã«ãã¤ã¤ãæ¢åã³ã¼ãã¯ãã®ã¾ã¾ã«ã§ããè¨è¨ã«ãªã£ã¦ãã¾ãããã10年以ä¸åãããªãã¨ãããªãããã¡ãã£ã¨ããã¹ã¯ãªããè¨èªã§æ¸ãããå ´åã«Perlãé¸æããã®ã¯ãæå¤ã¨çã«ããªã£ãé¸æããããã¾ããã
ææ¥18æ¥ç®ã¯xtetsujiããã§ãããããã½ã¼ã¹ãã£ã«ã¿ã«ææ¦ãã¦ã¿ãããªãã¨ãã§ããæ°ã«ãªãã¾ããã