Created
August 20, 2014 14:28
-
-
Save Code-Hex/eba5ab6d899914e33016 to your computer and use it in GitHub Desktop.
sub内の stateの勉強
This file contains 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/perl | |
use strict; | |
use warnings; | |
use 5.010; | |
my $sum; | |
running_sum(5,6); | |
running_sum(1..3); | |
running_sum(4); | |
sub running_sum { | |
state $num = 0; # state は以下の内容として宣言できる | |
state @numbers; # サブルーチン内の永続的なプライベート変数 | |
foreach my $number (@_){ | |
push @numbers, $number; | |
$sum += $number; | |
} | |
say "The sum of (@numbers) is $sum"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
state ってなくても良さそうw