Last active
April 11, 2016 07:41
-
-
Save note103/8919eff343ce79d24ed81d75d28614b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use strict; | |
| use warnings; | |
| use feature 'say'; | |
| # 1. 以下の無名サブルーチンがあったとして、 | |
| my $f = sub { | |
| my $s = shift; | |
| for my $i (@$s) { | |
| say $i; | |
| } | |
| }; | |
| # 2. 以下の配列リファレンスを「1」で回したいとき、 | |
| my $source = [qw/apple orange grape/]; | |
| # 3. 以下は実行されるが、 | |
| $f->($source); | |
| #=> apple | |
| #=> orange | |
| #=> grape | |
| #=> | |
| # 4. 以下は実行されないのが何故なのか、理解できていない。 | |
| m(); | |
| sub m { | |
| my $source = [qw/apple orange grape/]; | |
| $f->($source); | |
| } | |
| #=> Use of uninitialized value $_ in pattern match (m//) at /path/to/for-subroutine-practice.pl line 29. | |
| # 5. 関数名が 'm' だから駄目だった模様……。 | |
| n(); | |
| sub n { | |
| my $source = [qw/apple orange grape/]; | |
| $f->($source); | |
| } | |
| #=> apple | |
| #=> orange | |
| #=> grape | |
| #=> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ああ〜、なるほど。
mがその後の( )を正規表現のデリミタにしてしまっているんですね……なんで
mが駄目なのか、なんでパターンマッチとか言われているのか、などはわからないままでした。(組み込み関数のように、すでに予約されているのか? とか思ってました……)
(エラーメッセージでパターンマッチがどう、とか言っているのは一応読んでいたけどまったく想像力働かず)
で、おっしゃるとおり
&を使ったらちゃんと呼び出されました。把握!ありがとうございます!