#navi_header|Perl| - Perlには、C/PHP にあるようなstatic変数(関数内部の局所変数で、かつ、関数を抜けた後も値が保持される性質の変数)は存在しない。 - しかし、lexical変数は"{}"内のスコープを持っている為、これを利用して疑似static変数を使用できる。 - Perl >= 5.0? - コードピース #!/usr/bin/perl #use strict; <<<< 末尾のprintを使用しなければ有効化して大丈夫。 package test; { my $count; sub countup { $count++; $count; } } package main; print &test::countup(), "\n"; print &test::countup(), "\n"; print &test::countup(), "\n"; print &test::countup(), "\n"; print '$count is not defined', "\n" if !defined($count); print '$test::count is not defined', "\n" if !defined($test::count); - 出力 1 2 3 4 $count is not defined $test::count is not defined 只の"{}"でsubを囲む。lexical変数の場合、本当の意味で"機械的に"スコーピングしてくれる為、只の"{}"といえど、その中のmyはその中で保持される。これにより、"staticに見える"状況を創り出している。 #navi_footer|Perl|