Site menu:

Back to examples

Perl Example - A simple counter

#################################################
#Version 0.1                                    #
#Free to use for any purpose                    #
#No credits or backlink required                #
#Author : Shaji Kalidasan (shajiindia@yahoo.com)#
#################################################

use 5.010;
use warnings;
use autodie;

my $counter_file = "counter.dat";

my $counter = 0;

if(-f $counter_file) {
    open IN, '<', $counter_file;
    $counter = <IN>;
    chomp $counter;
    close IN;
}

++$counter;

if($counter == 1) {
    say "Welcome, You are running this file for the first time";
}else{
    say "Welcome back, You are running this file $counter times";
}

open OUT, '>', $counter_file;
print OUT $counter;
close OUT;