Skip to content

Instantly share code, notes, and snippets.

@ainame
Created October 9, 2011 08:58
Show Gist options
  • Save ainame/1273473 to your computer and use it in GitHub Desktop.
Save ainame/1273473 to your computer and use it in GitHub Desktop.
# !/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
# grep 関数の普通の使い方1
# 文字列の中にpが含まれている単語のフィルタリング
# grep EXP, LIST という構文
my @list = qw(perl ruby python php java c++);
my @result = grep /p/, @list;
warn Dumper @result;
# その2
# pが含まれている、かつ、4文字以上の単語
# 最後に評価された式が true なら @resultに返る
# grep BLOCK LIST という構文
# ↑間接メソッド呼び出し
@result = grep {
my $p = /p/;
if($p){
length($_) > 3;
}else{
0;
}
} @list;
warn Dumper @result;
# ブロックを与える間接メソッド呼び出しの作り方
# 1.サブルーチンを宣言するときにプロトタイプに&を書く
# 2.引数として受け取ったメソッドを->()で呼び出す
sub indirect_call(&$){
my @arg = @_;
$arg[0]->();
print "And fuck ".$arg[1]. "!\n";
}
# method_name BLOCK ARG と呼び出せる
indirect_call { print "thank you perl!!\n" } "perl";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment