Learn PERL with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
1
Perl Button Press Counter
my $count = 0;
sub updateUI {
my ($count) = @_;
print "Button Count: $count\n";
}
sub buttonPress { $count++; updateUI($count); }
# Simulate actions
updateUI($count);
buttonPress();
buttonPress();
Counts button presses and prints the count to the console.
2
Perl Theme Toggle
my $isDark = 0;
sub updateUI {
my ($isDark) = @_;
print "Theme: " . ($isDark ? 'Dark' : 'Light') . "\n";
}
sub toggleTheme { $isDark = !$isDark; updateUI($isDark); }
# Simulate actions
updateUI($isDark);
toggleTheme();
toggleTheme();
Toggles between Dark and Light themes.
3
Perl Score Tracker
my $score = 0;
sub updateUI { my ($score) = @_; print "Score: $score\n"; }
sub increment { $score += 10; updateUI($score); }
sub decrement { $score -= 5; updateUI($score); }
sub reset { $score = 0; updateUI($score); }
# Simulate actions
updateUI($score);
increment();
decrement();
reset();
Tracks a score with increment, decrement, and reset.
4
Perl Simple Timer
my $time = 0;
sub updateUI { my ($time) = @_; print "Time: $time sec\n"; }
sub tick { $time++; updateUI($time); }
# Simulate actions
updateUI($time);
tick();
tick();
Counts seconds and prints elapsed time.
5
Perl Health Tracker
my $health = 100;
sub updateUI { my ($health) = @_; print "Health: $health\n"; }
sub damage { $health -= 20; updateUI($health); }
sub heal { $health += 10; updateUI($health); }
# Simulate actions
updateUI($health);
damage();
heal();
Tracks health with damage and healing.
6
Perl Level Tracker
my $level = 1;
sub updateUI { my ($level) = @_; print "Level: $level\n"; }
sub nextLevel { $level++; updateUI($level); }
sub resetLevel { $level = 1; updateUI($level); }
# Simulate actions
updateUI($level);
nextLevel();
nextLevel();
resetLevel();
Tracks game levels with increment and reset.
7
Perl Coin Counter
my $coins = 0;
sub updateUI { my ($coins) = @_; print "Coins: $coins\n"; }
sub collectCoin { $coins++; updateUI($coins); }
sub loseCoin { $coins--; updateUI($coins); }
# Simulate actions
updateUI($coins);
collectCoin();
collectCoin();
loseCoin();
Counts coins collected and lost.
8
Perl Ammo Tracker
my $ammo = 10;
sub updateUI { my ($ammo) = @_; print "Ammo: $ammo\n"; }
sub shoot { $ammo--; updateUI($ammo); }
sub reload { $ammo = 10; updateUI($ammo); }
# Simulate actions
updateUI($ammo);
shoot();
shoot();
reload();
Tracks ammo usage with shoot and reload actions.
9
Perl Star Collector
my $stars = 0;
sub updateUI { my ($stars) = @_; print "Stars: $stars\n"; }
sub collectStar { $stars++; updateUI($stars); }
sub loseStar { $stars--; updateUI($stars); }
# Simulate actions
updateUI($stars);
collectStar();
collectStar();
loseStar();
Counts collected stars and displays progress.
10
Perl Power-Up Timer
my $powerTime = 10;
sub updateUI { my ($powerTime) = @_; print "Power-Up Time: $powerTime\n"; }
sub tick { $powerTime--; updateUI($powerTime); }
sub resetPower { $powerTime = 10; updateUI($powerTime); }
# Simulate actions
updateUI($powerTime);
tick();
tick();
resetPower();
Counts down power-up duration and resets.