Skip to content

Instantly share code, notes, and snippets.

@Code-Hex
Created August 20, 2014 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Code-Hex/eba5ab6d899914e33016 to your computer and use it in GitHub Desktop.
Save Code-Hex/eba5ab6d899914e33016 to your computer and use it in GitHub Desktop.
sub内の stateの勉強
#! /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";
}
@Code-Hex
Copy link
Author

state ってなくても良さそうw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment