æ¦è¦
- ç§ãPerlãå¦ç¿ããéã®åå¿ã¨ãã¦ã®ã¡ã¢ã§ã
- YouTubeã§é
ä¿¡ããã¦ããä¸è¨ã®åç»ããã¨ã«ä½æãã¦ããã¾ã
- Perlå ¥å¦å¼ ãªã³ã©ã¤ã³ 2021 第1å ãå°å ¥ç·¨ã
- Perlå ¥å¦å¼ ãªã³ã©ã¤ã³ 2021 第2å ãããã°ã©ãã³ã°åºç¤æ§æç·¨ã
- Perlå ¥å¦å¼ ãªã³ã©ã¤ã³ 2021 第3å ãé åé¢æ°/ããã·ã¥/ãµãã«ã¼ãã³ç·¨ã
- Perlå ¥å¦å¼ ãªã³ã©ã¤ã³ 2021 第4å ããªãã¡ã¬ã³ã¹/æ£è¦è¡¨ç¾ç·¨ã
- Perlå ¥å¦å¼ ãªã³ã©ã¤ã³ 2021 第5å ãã¢ããªä½æå®è·µç·¨ã
ç°å¢
- OS
- OracleLinux 9.1
- Perl
- 5.32.1
Table of Contents
- Perlãæ¸ãå§ããéã®ãã¾ããªãæ
- æåã®åºå
- ã¹ã«ã©ã¼å¤æ°
- ååæ¼ç®
- æååé£çµ
- æ¼ç®ã®é çª
- ã³ãã³ãã©ã¤ã³å¼æ°
- ifæ
- é å
- forã«ã¼ã
- é åã®æä½
- ããã·ã¥
- ãµãã«ã¼ãã³
- ãªãã¡ã¬ã³ã¹
- ããªãã¡ã¬ã³ã¹
- æ£è¦è¡¨ç¾
- Perlã§Web API
Perlãæ¸ãå§ããéã®ãã¾ããªãæ
- perlã§ããã°ã©ã ãæ¸ãéããã¡ã¤ã«ã®ä¸çªæåã«æ¸ããã¾ããªãæã§ãã
#!/usr/bin/env perl
: ãã®ããã°ã©ã ãperlã§åããã¨ãæ示ããè¨è¼use strict;
: å³å¯ãªæ¸å¼ã§æ¸ããã¨ãå¼·å¶ããæªå®ç¾©ã®å¤æ°ãè¦åããuse warnings;
: æã¾ããããªãè¨è¿°ãè¦åãã
#!/usr/bin/env perl use strict; use warnings;
æåã®åºå
- æåãåºåããå ´åã¯
print
ãå©ç¨ãã¦è¨è¼ãã
#!/usr/bin/env perl use strict; use warnings; print "Hello World!\n";
ã¹ã«ã©ã¼å¤æ°
- Perlã®å¤æ°ã«ã¯
ã¹ã«ã©å¤æ°
ãé å
ãããã·ã¥
ãããã¹ã«ã©å¤æ°($)
: 1ã¤ã®è¦ç´ ãæ ¼ç´ããé å(@)
: è¤æ°ã®è¦ç´ ãé çªã«æ ¼ç´ããããã·ã¥(%)
: è¤æ°ã®è¦ç´ ãåå=>ã®çµã¿åããã§æ ¼ç´ãã
å¤æ°ã®å©ç¨
- å¤æ°ãåãã¦ä½¿ãã¨ãã¯å
é ã«
my
ãã¤ãã - ã¹ã«ã©å¤æ°ã®å
é ã«ã¯
$
ãã¤ãã- $ã¯
scalar
ã®s
- $ã¯
#!/usr/bin/env perl use strict; use warnings; my $foo; $foo = 1; print "$foo\n"; $foo = "Hello World"; print "$foo\n";
- åºåçµæ
1 Hello World
- å¤æ°ã®å®£è¨ã¨åæã«å¤ãä»£å ¥ãããã¨ãã§ãã(ãã£ã¡ã主æµã£ã½ã)
#!/usr/bin/env perl use strict; use warnings; my $foo = 1; print "$foo\n";
ã¯ã©ã¼ãã¼ã·ã§ã³ã«ãã表示ã®éã
- å¤æ°ã®ä¸èº«ã表示ããå ´åã¯
"
(ããã«ã¯ã©ã¼ãã¼ã·ã§ã³)ã®ä¸ã«è¨è¼ãã - è¨è¼ãã¦ãããã®ã¾ã¾ã®å
容ã表示ããã«ã¯
'
(ã·ã³ã°ã«ã¯ã©ã¼ãã¼ã·ã§ã³)ã§è¨è¼ãã
#!/usr/bin/env perl use strict; use warnings; my $foo = 1; print "$foo\n"; print '$foo\n';
- åºåçµæ
1 $foo\n
ååæ¼ç®
- ã»ãã®è¨èªã®åããããªè¨è¼ã§åé¡ãªã
#!/usr/bin/env perl use strict; use warnings; my $foo = 4; my $bar = 2; my $result; # 足ãç® $result = $foo + $bar; print "$foo + $bar = "; print "$result\n"; # å¼ãç® $result = $foo - $bar; print "$foo - $bar = "; print "$result\n"; # æãç® $result = $foo * $bar; print "$foo * $bar = "; print "$result\n"; # å²ãç® $result = $foo / $bar; print "$foo / $bar = "; print "$result\n"; # ã¹ãä¹ $result = $foo ** $bar; print "$foo ** $bar = "; print "$result\n"; # å°ä½ $result = $foo % $bar; print "$foo % $bar = "; print "$result\n";
- åºåçµæ
4 + 2 = 6 4 - 2 = 2 4 * 2 = 8 4 / 2 = 2 4 ** 2 = 16 4 % 2 = 0
æååé£çµ
.
ã§æååãå¤æ°ãé£çµãããã¨ãã§ãã- é£çµããæååãå¤æ°ããå¥ã®å¤æ°ã«ä»£å ¥ãããã¨ãå¯è½
#!/usr/bin/env perl use strict; use warnings; my $word = 'word'; my $num = '123'; print "$word$num\n"; print $word . $num . "\n"; my $str = $word . $num; print $str . "\n";
- åºåçµæ
word123 word123 word123
æ¼ç®ã®é çª
- æ¼ç®ã®åªå é ä½ã¨çµåæ¹åã¯perldocã®perldoc.jp - Perlã®æ¼ç®åã¨åªå é ä½ã«è©³ç´°ãè¨è¼ããã¦ãã¾ã
- ã³ã¼ããè¦ã人ãç解ããããã«ãä¸è¨ã®ããã«ããããããè¨è¿°ããã
- ()ã§æ¼ç®ã®é çªãæ確ã«ç¤ºã
- æ°å¤æ¼ç®ã¨æååã®é£çµã¯åãã¦è¨è¼ãã
#!/usr/bin/env perl use strict; use warnings; my $num1 = 2 + 4 * 3; print "$num1\n"; my $num2 = ( 2 + 4 ) * 3; print "$num2\n"; # æ°å¤æ¼ç®ã¨æååæ¼ç®ãåããªãã§è¨è¼ããæªãä¾ my $now = 2024; my $my_birth_year = 2012; print "I am" . $now - $my_birth_year . "years old\n"; # æ°å¤æ¼ç®ã¨æååæ¼ç®ãåããä¾1 print "I am" . ( $now - $my_birth_year ) . "years old\n"; # æ°å¤æ¼ç®ã¨æååæ¼ç®ãåããä¾2 my $age = $now - $my_birth_year; print "I am" . $age . "years old\n";
- åºåçµæ
14 18 Argument "I am2024" isn't numeric in subtraction (-) at enzan_priority.pl line 17. -2012years old I am12years old I am12years old
ã³ãã³ãã©ã¤ã³å¼æ°
- ã³ãã³ãã©ã¤ã³å¼æ°ãè¨å®ããå ´åã¯ã¿ã¼ããã«ã§ä¸è¨ã®ããã«è¨è¼ãã¾ã
# å¼æ°ãï¼ã¤ã®å ´å perl single.pl "arg" # å¼æ°ãè¤æ°ã®å ´å perl multi.pl "arg1" "arg2"
- perlã§ã¯
shift
ã§ã³ãã³ãã©ã¤ã³å¼æ°ãåãåããã¨ãã§ãã¾ã
#!/usr/bin/env perl use strict; use warnings; my $first = shift; my $second = shift; print 'first is ' . "$first\n"; print 'second is ' . "$second\n";
- åºåçµæ(
perl cli_args.pl "aaa" "bbb"
ãå®è¡)
first is aaa second is bbb
ifæ
- Perlã«ã¯true/falseãããã¾ãã
- ã»ãã®ããã°ã©ãã³ã°è¨èªã§ã¯trueãfalseã¨ããåèªã«çã¾ãã¯å½ã®æå³ãæããããã¼ã¯ã¼ãã¨ãã¦æ±ããã¨ããã
- å¾è¿°ãã5ã¤ã®å½å¤ã®ã¿ãå½ããã以å¤ã¯ãã¹ã¦çã¨ãªãã¾ã
Perlã«ãããçã¨å½ã®å¤
- Perlã§ã¯æ¬¡ã®5ã¤ã®å¤ã
å½
ã¨ãã¦æ±ããããã以å¤ã®å¤ã¯ãã¹ã¦ç
ã¨ãã¦æ±ããã¾ã- æ°å¤
0
- æåå
0
- æåå
''
(ã·ã³ã°ã«ã¯ã©ã¼ãã¼ã·ã§ã³ã®é£ç¶ãéã«ä½ããªãã空æåã¨ããã) - 空ã®ãªã¹ã
()
- undef(å¤ãå ¥ã£ã¦ããªãã¹ã«ã©ã¼å¤æ°)
- æ°å¤
ifæã®æ§æ
if ( æ¡ä»¶å¼ ) { æ¡ä»¶å¼ã®çµæãçã®å ´åã«å®è¡ããããããã¯; } else { æ¡ä»¶å¼ã®çµæãå½ã®å ´åã«å®è¡ããããããã¯; }
- ãµã³ãã«ã³ã¼ã
#!/usr/bin/env perl use strict; use warnings; if ( 0 ) { print "OK\n"; } else{ print "NG\n" }
- åºåçµæ
NG
æ¯è¼æ¼ç®å
- æ°å¤ã®æ¯è¼æ¼ç®å
== # å³è¾ºã¨å·¦è¾ºãçãããªãã°ç != # å³è¾ºã¨å·¦è¾ºãçãããªããªãã°ç < # å³è¾ºãã左辺ãå°ãããªãã°ç > # å³è¾ºãã左辺ã大ãããªãã°ç <= # å³è¾ºã左辺以ä¸ãªãã°ç >= # å³è¾ºã左辺以ä¸ãªãã°ç
- æååã®æ¯è¼æ¼ç®å
lt
ãgt
ã§æ¯è¼ããæååã®å¤§å°ã¯æååã®é·ãã§ã¯ãªããè¾æ¸é ã«ä¸¦ã¹ãæã«åã«ãããã®ãå°ãããå¾ãã«æ¥ããã®ã大ããã¨å¤æããã¾ã
eq # equal ne # not equal gt # greater than ge # greater equal lt # less than le # less equal
-
è¤æ°ã®æ¡ä»¶åå²
elsif
ã使ããã¨ã§ãåå²æ¡ä»¶ãå¢ãããã¨ãã§ãã¾ãelse if
ã§ã¯ãªãelsif
ãªã®ã§è¦æ³¨æ(ã»ãã®è¨èªã¨æ··åãã¡ãããã)
-
ãµã³ãã«ã³ã¼ã
#!/usr/bin/env perl use strict; use warnings; my $num = 3; if ($num == 1 ) { print "OK\n"; } else{ print "NG\n" } my $foo = 'hello'; if ($foo eq 'hello' ) { print "OK\n"; } else{ print "NG\n" } if ($num == 1) { print "$num is One" . "\n"; } elsif ($num == 2 ) { print "$num is Two" , "\n"; } elsif ($num == 3 ) { print "$num is Three" , "\n"; } else { print "$num is not even One, even Two, even Three" , "\n"; }
- åºåçµæ
NG OK 3 is Three
è«çæ¼ç®å
- ifæã®æ¡ä»¶å¼ã§ããè¤éãªæ¡ä»¶ãæ±ãããã¨ãã¯è«çæ¼ç®åãç¨ãã¦è¤æ°ã®æ¡ä»¶ãé£çµãã¾ã
&& # ã㤠|| # ã¾ãã¯
&&
æ¼ç®åã¯ããã¤ã»ANDãã®æå³ãæã¤ã&&
ã®ä¸¡è¾ºã®æ¡ä»¶ãçã¨ãªãã¨ãã®ã¿çã¨ãªã-
||
æ¼ç®åã¯ããã¾ãã¯ã»ORãã®æå³ãæã¤ã||
両辺ã®ãã¡ãçæ¹ãçãªãã°çã¨ãªã ãµã³ãã«ã³ã¼ã
#!/usr/bin/env perl use strict; use warnings; my $num = 10; if ( $num > 0 && $num % 2 == 0 ) { print "&& : OK\n"; } else{ print "&& : NG\n"; } if ( $num > 0 || $num % 2 == 1) { print "|| : OK\n"; } else{ print "|| : NG\n"; } my $small = 10; my $medium = 20; my $large = 30; if ( $small < $medium <= $large ) { print "å°ããé ã«ä¸¦ãã§ãã¾ã\n"; }
- åºåçµæ
&& : OK || : OK å°ããé ã«ä¸¦ãã§ãã¾ã
é å
- è¤æ°ã®å¤ãè¤æ°ã®ã¹ã«ã©ã¼å¤æ°ãã¾ã¨ãã¦æ±ããã¨ãã§ãããã®
- é
åãä½ãå ´åã¯å¤æ°åã®åã«
@
ãã¤ãã¾ã@
ã¯ã¢ã¬ã¤(array)ã@rrayã¨è¦ããã¨ãããããã§ã
- é
åããè¦ç´ ãå©ç¨ããéã¯æ·»åã使ãã¾ã
- é åã®æ·»åã¯æ°åã§æå®ãã¾ã
- é åã®å é ã®è¦ç´ ã®æ·»åã¯0ã¨ãªããæ«å°¾ã«åãã¦1ãã¤å¢ãã¾ã
- é åã®è¦ç´ ã«ä»£å ¥ãããã¨ãã§ãã¾ã
- é åã«å¥ã®é åã®ä¸èº«ã追å ãããã¨ãã§ãã¾ã
- é åã®è¦ç´ æ°ãåå¾ããå ´åã¯ãscalaré¢æ°ã®å¼æ°ã«é åãä¸ãããã¨ã§è¦ç´ ã®åæ°ãåå¾ã§ãã¾ã
- é
å
@array
ã«å¯¾ãã¦@#array
ã¨æ¸ããã¨ã§ãé åã«æ ¼ç´ããã¦ããæ«å°¾ã®è¦ç´ ã®æ·»åãåå¾ã§ãã¾ã - é£ç¶ããæ°åãé
åã«æ ¼ç´ããã¨ãã¯ç¯å²æ¼ç®å
..
ã便å©ã§ã(1 .. 5)- å·¦ã®æ°å¤ããå³å´ã®æ°å¤ã大ãããã¨ãæ¡ä»¶ã«ãªãã¾ã
#!/usr/bin/env perl use strict; use warnings; my @array = ( 1, "foo", 3 ); # é åã¨é åã®è¦ç´ åºå print "@array" . "\n"; print "$array[0]" . "\n"; print "$array[1]" . "\n"; print "$array[2]" . "\n"; # é åã®è¦ç´ ã«ä»£å ¥ $array[0] = "bar"; print "@array" . "\n"; # é åã«å¥ã®é åã®è¿½å my @array_one_two = ( 1, 2 ); my @array_numbers = ( @array_one_two, 3 ); print "@array_numbers" . "\n"; # é åã®è¦ç´ æ°ã®åå¾ my $count_array_element = scalar @array; print "$count_array_element\n"; # é åã®æ«å°¾ã®è¦ç´ ã®æ·»å my $last_array_index = $#array; print "$last_array_index\n"; # ç¯å²æ¼ç®å my @array1 = ( 1 .. 5 ); print "@array1\n";
- åºåçµæ
1 foo 3 1 foo 3 bar foo 3 1 2 3 3 2 1 2 3 4 5
forã«ã¼ã
- é åã®è¦ç´ ãé çªã«ãã¹ã¦å¦çããå ´åã«foræãããã¾ã
- foræã¨ç¯å²æ¼ç®åãçµã¿åããããã¨ã§ã決ã¾ã£ãåæ°å¦çãç¹°ãè¿ããã¨ãã§ãã¾ã
#!/usr/bin/env perl use strict; use warnings; my @array = ( 1, "foo", 3 ); for my $element (@array){ print "$element\n"; } # ã«ã¦ã³ãã¢ãã for my $i ( 1 ... 5 ){ print "$i\n"; } # åãªãç¹°ãè¿ã for my $i ( 1 ... 5 ){ print "Hello world\n"; }
- åºåçµæ
1 foo 3 1 2 3 4 5 Hello world Hello world Hello world Hello world Hello world
é åã®æä½
é åæä½ã®é¢æ°
- join, split
- é åãã¹ã«ã©ã¼å¤ã«ãã¹ã«ã©ã¼å¤ãé åã«ãã
- sort, reverse
- é åã並ã¹æ¿ãããéé ã«ãã
- pop, shirt, push, unshift
- é åããè¦ç´ ãåãåºããè¦ç´ ã追å ãã
join
- ãªã¹ããé
åã®è¦ç´ ãé£çµãã¦ãã¹ã«ã©ã¼å¤ã«ããé¢æ°ã§ã
- joinãåãåã第1å¼æ°ã¯ããªã¹ããé åã®è¦ç´ ããã£ã¤ããç³ã®ãããªå½¹å²ãæããã¾ã
- 第2å¼æ°ã«ã¯ã対象ã®é åããªã¹ãã渡ãã¾ã
#!/usr/bin/env perl use strict; use warnings; my @words = ( 'I', 'Love', 'Perl.' ); my $poem = join '_', @words; print $poem . "\n"
- åºåçµæ
I_Love_Perl.
split
- æå®ãããã¿ã¼ã³ã«å¾ã£ã¦ã¹ã«ã©ã¼å¤ãæååãåå²ãããªã¹ãã«ãã¾ã
- splitãåãåã第1å¼æ°ã¯ãæååãåå²ããåºåãã®ãããªå½¹å²ãæããã¾ã
- 第2å¼æ°ã«ã¯ã対象ã®æååã渡ãã¾ã
/ /
ã¯æ£è¦è¡¨ç¾ãªãã©ã«ã¨å¼ã°ãããã®ã§ãããã®2ã¤ã®ã¹ã©ãã·ã¥ã®éã«ãããã®ã§æååãåå²ãã¾ã
#!/usr/bin/env perl use strict; use warnings; my $poem = 'I_Love_Perl.'; my @words = split /_/, $poem; print "@words\n"
- åºåçµæ
I Love Perl.
sort
- é åãã«ã¼ã«é ã«ä¸¦ã¹æ¿ãã¦ããã®é åãè¿ãã¾ã
sort
ã®ã¿ããããã¯sort { $a cmp $b } @array
ã¨æ¸ãã¨ãæååã¨ãã¦æé ã«ä¸¦ã¹æ¿ãã¾ã- æ°å¤ãæé ã«ä¸¦ã¹æ¿ããã¨ãå
é ã®æåã§å¤æãã¦ä¸¦ã¹æ¿ãããã¾ãã
- æ°å¤ã®å¤§å°ã§ä¸¦ã¹æ¿ããå ´åã¯
sort { $a <=> $b } @array
ã¨è¨è¼ãã¾ã - å¤æ°
$a
ã¨$b
ã¯sortã§ä½¿ãããã«äºç´ããã¦ããã®ã§ãsort以å¤ã§ä½¿ããªãããã«ãã¾ããã
- æ°å¤ã®å¤§å°ã§ä¸¦ã¹æ¿ããå ´åã¯
#!/usr/bin/env perl use strict; use warnings; my @lang = ( 'perl', 'php', 'ruby', 'python', 'java', 'go' ); my @sorted_lang = sort @lang; print "@sorted_lang" . "\n"; my @num = ( 5, 200, 40, 3, 1 ); my @sorted_num = sort @num; print "@sorted_num" . "\n"; my @sorted = sort { $a <=> $b } @num; print "@sorted" . "\n";
- åºåçµæ
go java perl php python ruby 1 200 3 40 5 1 3 5 40 200
reverse
- ãªã¹ããéé ã«ä¸¦ã¹æ¿ãã¦ããã®ãªã¹ããè¿ãé¢æ°ã§ã
#!/usr/bin/env perl use strict; use warnings; my @array = reverse ( 1 .. 5 ); print "@array" . "\n";
- åºåçµæ
5 4 3 2 1
push/pop
- push/popã¯é åã®æ«å°¾ã®è¦ç´ ãæä½ããé¢æ°ã§ã
- é åã®æ«å°¾ã«è¦ç´ ã追å ããã¨ãã¯pushãå©ç¨ãã¾ã
- é åã®æ«å°¾ããè¦ç´ ãåãåºãã¨ãã¯popãå©ç¨ãã¾ã
#!/usr/bin/env perl use strict; use warnings; my @members = ( 'Alice', 'Bob' ); push @members, 'Carol'; print "@members" . "\n"; my @member = pop @members; print "@members" . "\n"; print "@member" . "\n";
- åºåçµæ
Alice Bob Carol Alice Bob Carol
unshift/shift
- unshift/shiftã¯é åã®å é ã®è¦ç´ ãæä½ããé¢æ°ã§ã
- é åã®å é ã«è¦ç´ ã追å ããã¨ãã¯unshiftãå©ç¨ãã¾ã
- é åã®å é ããè¦ç´ ãåãåºãã¨ãã«ã¯shiftãå©ç¨ãã¾ã
#!/usr/bin/env perl use strict; use warnings; my @members = ( 'Alice', 'Bob' ); unshift @members, 'Carol'; print "@members" . "\n"; my @member = shift @members; print "@members" . "\n"; print "@member" . "\n";
- åºåçµæ
Carol Alice Bob Alice Bob Carol
ããã·ã¥
ããã·ã¥ã®ä½æ
- perlã®ãã¼ã¿æ§é ã®1ã¤ã§ãé åã¨åããè¦ç´ ã®æ ¼ç´ã»åãåºããã§ãã¾ã
- ãã ããé åã¨ç°ãªãåå(key)ã¨å¤(value)ã®ãã¢ã§æ ¼ç´ããã¾ã
- ãã®ãã¢ã®ãã¨ãè¦ç´ ã¨å¼ã³ã¾ã
- åå(key)ã¨å¤(value)ã®éã«ãã
=>
ã¯ãã¡ããã³ã³ãæ¼ç®åã¨å¼ã°ããã³ã³ãã¨åçã®å½¹å²ãæããã¾ã- ãã®ãã
,
ã§ãåä½ãã¾ãããé åã¨ã®è¦åããé£ãããªãã¾ã
- ãã®ãã
# ã«ã³ãã§è¨è¼ my %hash = ( 'name)', 'Larry', 'birth', 1954, ); # ãã¡ããã³ã³ãæ¼ç®åã§è¨è¼ my %hash = ( 'name' => 'Larry', 'birth' => 1954, );
- åå(key)ã¯æååã¨è§£éããã¾ã
- ãã®ããã·ã³ã°ã«ã¯ã©ã¼ãã¼ã·ã§ã³ãããã«ã¯ã©ã¼ãã¼ã·ã§ã³ã§å²ãå¿ è¦ã¯ããã¾ãã
- ããã·ã¥ããè¦ç´ ãåãåºãå ´åã¯æ³¢æ¬å¼§
{ }
ã使ãã¾ã
my %hash = ( name => 'Larry', birth => 1954, ); print "$hash{name}\n"; print "$hash{birth}\n";
- ããã·ã¥ã«è¦ç´ ã追å ããå ´åã¯åãåºãã¨ãã¨åæ§ã«{key}ã使ãã¾ã
my %hash = ( name => 'Larry', birth => 1954, ); $hash{lang} = 'Perl'; print "$hash{lang}\n";
- ããã·ã¥ã®ä¸èº«ãå
¨é¨è¦ãå ´åã¯
Data::Dumper
ã¢ã¸ã¥ã¼ã«ãå©ç¨ãã¾ã
use Data::Dumper; my %hash = ( name => 'Larry', birth => 1954, lang => 'Perl', );
ããã·ã¥ã®æä½
- keys
- ããã·ã¥ã®åå(key)ã®éåãè¿ã
- delete
- ããã·ã¥ã®è¦ç´ ãåé¤ãã
- exists
- ããã·ã¥ã®è¦ç´ ãåå¨ãããããªãããè¿ã
keys
- ããã·ã¥ã®åå(key)ãé
åã«ãã¦è¿ãã¾ã
- keysã¯åå(key)ãé ä¸åãé çªãä¸å®ã§è¿ãã¾ã
#!/usr/bin/env perl use strict; use warnings; my %hash = ( name => 'Larry', birth => 1954, lang => 'Perl', ); my @keys = keys %hash; print "@keys\n"
- åºåçµæ
birth name lang
delete
- æå®ããããã·ã¥ã®åå(key)ã¨ãããã«å¯¾å¿ããå¤(value)ãåé¤ãã¾ã
#!/usr/bin/env perl use strict; use warnings; my %hash = ( name => 'Larry', birth => 1954, lang => 'Perl', ); delete $hash{lang}; print "$hash{lang}\n"
- åºåçµæ
Use of uninitialized value $hash{"lang"} in concatenation (.) or string at hash_delete.pl line 12.
exist
- æå®ããããã·ã¥ã®åå(key)ãåå¨ããã確èªãã¾ã
- åå(key)ãåå¨ããã°1(ç)ãè¿ãã¾ã
- åå(key)ãåå¨ããªããã°' '(空æåãå½)ãè¿ãã¾ã
#!/usr/bin/env perl use strict; use warnings; my %hash = ( name => 'Larry', birth => 1954, lang => 'Perl', ); if ( exists $hash{name} ) { print "exists\n" } if ( exists $hash{foo} ) { print "exists\n" }
- åºåçµæ
exists
ãµãã«ã¼ãã³
ãµãã«ã¼ãã³ã¨ã¯ï¼
- ããã°ã©ã ã®ä¸ã§ãæå³ãå 容ãã¾ã¨ã¾ã£ã¦ããä½æ¥ãã²ã¨ããã¾ãã«ãããã®ããµãã«ã¼ãã³ã¨å¼ã³ã¾ã
- Perlã«ããããµãã«ã¼ãã³ã¯ãé¢æ°ãã¨ã»ã¼å義ã§ã
ãµãã«ã¼ãã³ã®å®ç¾©
- ãµãã«ã¼ãã³ãå®ç¾©ããã«ã¯
sub ãµãã«ã¼ãã³å { ... }
ã¨æ¸ãã¾ã- å®ç¾©æã«æ«å°¾ã«
;
ã¯ä¸è¦ã§ã
- å®ç¾©æã«æ«å°¾ã«
ãµãã«ã¼ãã³ã®å¼ã³åºã
- å®ç¾©ãããµãã«ã¼ãã³ã¯å®ç¾©ãããµãã«ã¼ãã³åã®å¾ãã«()ãã¤ãããã¨ã§ãããã§ãã¾ã
ãµãã«ã¼ãã³å();
- ãµãã«ã¼ãã³ã«å¤(å¼æ°)ã渡ãããå ´åã()ã®ä¸ã«æ¸ãã¾ã
- ãµãã«ã¼ãã³ã«ä¸ããããå¼æ°ã¯
@_
ã¨ããé åã«æ ¼ç´ããã¾ã@_
ã¯çç¥ãããã¨ãã§ãã¾ã
sub say { my $str = shift @_; print "$str\n"; } say('Hello Perl');
- ãµãã«ã¼ãã³ã«è¤æ°ã®å¼æ°ã渡ããã¨ãã§ãã¾ã
sub plus { my ($first, $second) = @_; return $first + $second } my $result = plus(2, 5); print $result . "\n";
ãªãã¡ã¬ã³ã¹
- ãªãã¡ã¬ã³ã¹ãå©ç¨ãããã¨ã§é åãããã·ã¥ãã¹ã«ã©ã¼å¤æ°ã¨ãã¦æ±ããã¨ãã§ãã¾ã
- ãªãã¡ã¬ã³ã¹ã®ä½ãæ¹ã¯ä»¥ä¸ã®2ã¤ãããã¾ã
- é
åã»ããã·ã¥ãä½ããããããªãã¡ã¬ã³ã¹åãã¦ã¹ã«ã©ã¼å¤æ°ã«ããæ¹æ³
- é
åã»ããã·ã¥ããªãã¡ã¬ã³ã¹åããã«ã¯å
ã®å¤æ°ã«
\
(ããã¯ã¹ã©ãã·ã¥)ãã¤ãã¦ã¹ã«ã©ã¼ã«ä»£å ¥ãã¾ã
- é
åã»ããã·ã¥ããªãã¡ã¬ã³ã¹åããã«ã¯å
ã®å¤æ°ã«
- é
åã»ããã·ã¥ãä½ããããããªãã¡ã¬ã³ã¹åãã¦ã¹ã«ã©ã¼å¤æ°ã«ããæ¹æ³
my @alice_source = ( 60, 90 ); my $alice_source_ref = \@alice_source;
my %alice = ( name => 'Alice', japanese => 60, math => 90 ); my $alice_ref = \%alice;
- ç¡åé
åã»ç¡åããã·ã¥ã¨ãã¦ãã¼ã¿ãç´æ¥ãªãã¡ã¬ã³ã¹ã¨ãã¦æ ¼ç´ããæ¹æ³
- é
åã®å ´åã¯å³ã®ãªã¹ãã
[ ]
ã§å²ã - ããã·ã¥ã®å ´åã¯å³è¾ºã®ãªã¹ãã
{ }
ã§å²ã
- é
åã®å ´åã¯å³ã®ãªã¹ãã
my $alice_source_ref = [ 60, 90 ];
my $alice_ref = { name => 'Alice', japanese => 60, math => 90 };
- ãªãã¡ã¬ã³ã¹ã§è¤éãªãã¼ã¿æ§é ãä½æ
- ãªãã¡ã¬ã³ã¹ã¯printã ãã§ã¯ç¢ºèªã§ããªãããData::Dumperã¢ã¸ã¥ã¼ã«ãå©ç¨ãã¾ã
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my %alice = ( name => 'Alice', japanese => 60, math => 90 ); my $alice_ref = \%alice; my %bob = ( name => 'Bob', japanese => 60, math => 90 ); my $bob_ref = \%bob; my %charie = ( name => 'Charie', japanese => 60, math => 90 ); my $charie_ref = \%charie; my @array = ( $alice_ref, $bob_ref, $charie_ref ); print Dumper \@array;
ããªãã¡ã¬ã³ã¹
- ãªãã¡ã¬ã³ã¹åãã¦ã¹ã«ã©ã¼å¤æ°ã«ãããã¼ã¿ããå ã®é åã»ããã·ã¥ã«æ»ããã¨ãããªãã¡ã¬ã³ã¹ã¨ããã¾ã
- ã¢ãã¼è¨æ³ãç¨ãããã¨ã§ç°¡æãªè¨æ³ã§ãã¼ã¿ãåãåºããã¨ãå¯è½ã§ã
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; # é åãããªãã¡ã¬ã³ã¹ my @alice_source = ( 60, 90 ); my $alice_source_ref = \@alice_source; my @alice_source_deref = @{$alice_source_ref}; print $alice_source_deref[0] . "\n"; # ã¢ãã¼è¨æ³ã§é åã®ãã¼ã¿ãåãåºã print $alice_source_ref->[0] . "\n"; # ããã·ã¥ãããªãã¡ã¬ã³ã¹ my %alice = ( name => 'Alice', japanese => 60, math => 90 ); my $alice_ref = \%alice; my %alice_deref = %{$alice_ref}; print $alice_deref{name} . "\n"; # ã¢ãã¼è¨æ³ã§ããã·ã¥ã®ãã¼ã¿ãåãåºã print $alice_ref->{name} . "\n";
æ£è¦è¡¨ç¾
- Perlã¯ä»ã®ããã°ã©ãã³ã°è¨èªã¨æ¯ã¹ã¦æ£è¦è¡¨ç¾ãæ¸ããããè¨èªã§ãã
- æ£è¦è¡¨ç¾ãå©ç¨ãã¦æ¡ä»¶ã«åè´ãããããã¹ãããããã«ã¯ä»¥ä¸ã®ããã«å°ç¨ã®æ¼ç®åãå©ç¨ãã¾ã
=~
ã®å·¦è¾ºã«æ¯è¼ãããæååãè¨è¿°- å³è¾ºã«æ£è¦è¡¨ç¾ã
//
ã§å²ã£ã¦è¨è¿°
#!/usr/bin/env perl use strict; use warnings; # pã§å§ã¾ãlã§çµãã4æåã®åèªãããããããã®ãã¹ã if ('perl' =~ /p..l/){ print "match" . "\n"; }else{ print "no match" . "\n"; }
- æ£è¦è¡¨ç¾ã
()
ã§å²ããã¨ã§ãããããæååãåå¾ã§ãã¾ã(ãã£ããã£ã¨å¼ã¶)- ãã£ããã£ãããæååã¯
$1
ã¨ããç¹æ®å¤æ°ã«æ ¼ç´ããã¾ã - è¤æ°ãã£ããã£ãããã¨ãå¯è½ã§ããããã®æã¯ãããããæååã$1,$2...ã¨é çªã«å¤æ°ã«æ ¼ç´ããã¾ã
- ãã£ããã£ãããæååã¯
# æ£è¦è¡¨ç¾ã«ãããããæååãåå¾ if ('perl girl' =~ /(p..l)/){ print $1 . "\n"; }else{ print "no match" . "\n"; }
- åå¾ãããæååã¯æ±ºã¾ã£ã¦ããããããã¨ã¯å¥ã«ã°ã«ã¼ãåã ããã¦ããããå ´é¢ã§ã¯ãã£ããã£ããªãã°ã«ã¼ãåã§ãã
(?:)
ãå©ç¨ãã
if ('perl girl' =~ /(?:p..l) (g..l)/){ print $1 . "\n"; }else{ print "no match" . "\n"; }
- æ£è¦è¡¨ç¾ãç¨ãã¦ãããããæååã®ç½®æãè¡ããã¨ãã§ãã¾ã
my $greet = "Hello! Alice"; $greet =~ s/Alice/Perl/; print $greet . "\n";
- åºåãè¨æ³ãå¤æ´ãããã¨ãã§ãã¾ã
- æ£è¦è¡¨ç¾ã§ç¨ããåºåãæå
/
ã¨URLãpathã®åºåãæåã§ãã/
ãæ··å¨ããå ´åã¯{}
ã«å¤æ´ã§ãã¾ã - ããããæ¢ãéã
m{}
ã¨æ¸ããã¨ã§åºåãæåãå¤æ´å¯è½ã§ã
- æ£è¦è¡¨ç¾ã§ç¨ããåºåãæå
my $path = '/usr/local/bin/perl'; $path =~ s{/usr/local/bin/}{/usr/bin/}; print $path . "\n"; if ('perl girl' =~ m{p..l}) { print "match" . "\n"; }else{ print "no match" . "\n"; }
- 大æåå°æååããæ¤ç´¢ãããå ´åperlã§ã¯i修飾åã使ããã¨ã§å¤§æåå°æåãåºå¥ããã«æ¤ç´¢ã§ãã¾ã
if ('Perl' =~ /p..l/i) { print "match" . "\n"; }else{ print "no match" . "\n"; }
Perlã§Web API
HTTP:Tiny
HTTP:Tiny
ãå©ç¨ãã¦Web APIãå©ç¨ãã¾ã
JSON:PP
JSON:PP
ã¢ã¸ã¥ã¼ã«ãå©ç¨ãã¦JSONå½¢å¼ã®ãã¼ã¿ãPerlã®ãã¼ã¿å½¢å¼ã«å¤æãã¾ã
æ°è±¡åºéå ¬å¼Web APIãå©ç¨
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use HTTP::Tiny; use JSON::PP; my $url = 'https://www.jma.go.jp/bosai/forecast/data/overview_forecast/130000.json'; my $response = HTTP::Tiny->new()->get($url); my $content = $response->{content}; my $decoded_content = JSON::PP->new->decode($content); print $decoded_content->{targetArea};