Successfully reported this slideshow.
Upcoming SlideShare
×

# Perlのsmart match演算子

453 views

Published on

Review of Smart match for LT at Hatena on 2013/08/23.

Published in: Sports, Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Perlのsmart match演算子

1. 1. ~~ in perl id:astj
2. 2. Ø| m−oó\$aw =ê@b”Ì• m−v−ß¤o x
3. 3. ž \$a = 3; @b = (1,2,3,4); foreach \$elem (@a) { return 'Hit!' if \$elem == \$a; } «ł› ¾Í–ƒq grep { \$a == \$_ } @b;
4. 4. perl 5.10 ı “
5. 5. ~~/æÄ
6. 6. \$a ~~ @b @b”Ì•\$awm− xÕþßç•þáÞ|—¡ use 5.010; \$a = 3; @b = (1,2,3,4); print "Hit!n" if \$a ~~ @b;
7. 7. Õþßç ¡„
8. 8. \$a ~~ %c îáÓ™”ô»Éß³¢—¡ use 5.010; \$a = "Steve"; %c = ( "Steve" => "Jobs", "Bill" => "Gates" ); print "Hit!n" if \$a ~~ %c;
9. 9. oo ¡„
10. 10. m› ô¹ Õ Øq÷ö ¡ (Ø|v|¤œ5.14m¤ßvœvØ) \$a = 3; \$b = (1,2,3,4); print "Hit!n" if \$a ~~ \$b; «¶Æ•5.14vœpush vpop vkeys vØ ô¹ Õ −œ|o ¡„
11. 11. \$a ~~ \$d \$a == \$dºœ\$a eq \$dºœ ø|¶•́°©—¡
12. 12. ̂̂̂¶µvó^ ¡„
13. 13. @a ~~ @b \$a = [1,2,3]; \$b = [1,2,3]; print "Smart match!n" if \$a ~~ \$b; # match! ž O \$p = [ 1, 2, 3 ]; \$q = [ 1, 2, 3, 4 ]; print "Smart match!n" if \$p ~~ \$q; # matchしない! \$r = [ 1, 2, 3 ]; \$s = [ 1, 2, [ 3, 4, 5 ] ]; print "Smart match!n" if \$r ~~ \$s; # matchするんだなぁ µµ̇
14. 14. perl 5.16”perldoc¢− Right operand is an ARRAY: Left Right Description and pseudocode =============================================================== ARRAY1 ARRAY2 recurse on paired elements of ARRAY1 and ARRAY2[2] like: (ARRAY1[0] ~~ ARRAY2[0]) && (ARRAY1[1] ~~ ARRAY2[1]) && ... HASH ARRAY any ARRAY elements exist as HASH keys like: grep { exists HASH->{\$_} } ARRAY Regexp ARRAY any ARRAY elements pattern match Regexp like: grep { /Regexp/ } ARRAY undef ARRAY undef in ARRAY like: grep { !defined } ARRAY Any ARRAY smartmatch each ARRAY element[3] like: grep { Any ~~ \$_ } ARRAY
15. 15. =êœÀ”~~“ oóÀ•~~|–−” \$a = [ 1, 2, 3 ]; \$b = [ 1, 2, [ 3, 4, 5 ] ]; •u¦−(\$a ~~ \$b) »ﬁ—ß ( 1 ~~ 1 ) && ( 2 ~~ 2 ) && ( 3 ~ [3,4,5] ) # TRUE && TRUE && TRUE
16. 16. ž O∼Œ¶z¶›–x¤
17. 17. îáÓ™» Left Right Description and pseudocode =============================================================== HASH1 HASH2 all same keys in both HASHes like: keys HASH1 == grep { exists HASH2->{\$_} } keys HASH1 ARRAY HASH any ARRAY elements exist as HASH keys like: grep { exists HASH->{\$_} } ARRAY Regexp HASH any HASH keys pattern match Regexp like: grep { /Regexp/ } keys HASH undef HASH always false (undef can''t be a key) like: 0 == 1 Any HASH HASH key existence like: exists HASH->{Any}
18. 18. Keysm›–© Value»∙q Øooœ|o \$a = { "Steve" => "Jobs", "Bill" => "Gates" }; \$b = { "Steve" => "Ballmer", "Bill" => "Gates" }; print "Smart match!n" if \$a ~~ \$b; # matchする!!
19. 19. ∙µ∙µ |z¶›–x¤
20. 20. H•Øo¬o¬ |z–ºº̃|z–
21. 21. —ÞO•Øº }œ|o” ( id:moznion œ) oÚ¹ô½ßèïáË: ̃©̌” Èo |¤
22. 22. The smartmatch family of features are now experimental