Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
A number guessing game is a classic beginner's project, and implementing it in Perl provides a great way to understand basic programming concepts such as loops, conditional statements, and user input.
The computer randomly selects a number between 1 and 100, and the player tries to guess it.
#!/usr/bin/perl use strict; use warnings; # 1. Generate a random number between 1 and 100 my $random_number = int(1 + rand 100); my $guess; my $attempts = 0; print "Welcome to the Number Guessing Game!\n"; print "I've chosen a number between 1 and 100. Try to guess it!\n"; while (1) { # Infinite loop to keep asking the user for a guess until they're correct or choose to exit. # 2. Prompt the user to guess the number print "Enter your guess (or 'exit' to quit): "; $guess = <STDIN>; chomp($guess); # Removing newline character from user input # Check if the user wants to exit if ($guess eq "exit") { print "The number was $random_number. Better luck next time!\n"; last; } # Ensure the input is a number if ($guess !~ /^\d+$/) { print "Please enter a valid number!\n"; next; } $attempts++; # 3. Check the guess if ($guess == $random_number) { print "Congratulations! You've guessed the correct number in $attempts attempts.\n"; last; # Exit the loop as the game is over } elsif ($guess < $random_number) { print "Too low! Try again.\n"; } else { print "Too high! Try again.\n"; } }
guess_game.pl
.perl guess_game.pl
This game introduces basic Perl concepts like loops, conditional statements, user input processing, and random number generation. It's a fun and educational way to get a taste of programming in Perl.
Creating a simple guessing game in Perl:
# Simple Guessing Game in Perl use strict; use warnings; # Generate a random number between 1 and 10 my $secret_number = int(rand(10)) + 1; # Prompt user for a guess print "Guess the number (between 1 and 10): "; my $user_guess = <STDIN>; chomp $user_guess; # Check if the guess is correct if ($user_guess == $secret_number) { print "Congratulations! You guessed the correct number.\n"; } else { print "Sorry, the correct number was $secret_number.\n"; }
Perl random number generation for games:
rand()
function to generate random numbers for games.# Generate a random number between 1 and 100 my $random_number = int(rand(100)) + 1;
User input handling in Perl for games:
STDIN
and chomp
for cleaner input processing.# Prompt user for input and remove newline character print "Enter your guess: "; my $user_input = <STDIN>; chomp $user_input;
Conditional statements in Perl for guessing game logic:
if-else
statements to implement game logic based on user input.if ($user_guess == $secret_number) { print "Congratulations! You guessed the correct number.\n"; } else { print "Sorry, the correct number was $secret_number.\n"; }
Loop structures in Perl for game repetition:
while
or for
for repeated gameplay.# Play the game until the user guesses correctly while ($user_guess != $secret_number) { # Game logic... # Prompt user for a new guess print "Try again. Enter your guess: "; $user_guess = <STDIN>; chomp $user_guess; }
Perl script for a basic number guessing game:
use strict; use warnings; # Generate a random number between 1 and 10 my $secret_number = int(rand(10)) + 1; # Play the game until the user guesses correctly while (1) { print "Guess the number (between 1 and 10): "; my $user_guess = <STDIN>; chomp $user_guess; if ($user_guess == $secret_number) { print "Congratulations! You guessed the correct number.\n"; last; # Exit the loop } else { print "Sorry, try again.\n"; } }
Adding scoring and feedback to Perl guessing game:
use strict; use warnings; my $secret_number = int(rand(10)) + 1; my $attempts = 0; while (1) { print "Guess the number (between 1 and 10): "; my $user_guess = <STDIN>; chomp $user_guess; $attempts++; if ($user_guess == $secret_number) { print "Congratulations! You guessed the correct number in $attempts attempts.\n"; last; } else { print "Sorry, try again.\n"; } }
Interactive console-based games in Perl:
use strict; use warnings; sub play_guessing_game { # Game logic... } sub play_another_game { # Game logic... } # Main menu while (1) { print "Main Menu:\n"; print "1. Play Guessing Game\n"; print "2. Play Another Game\n"; print "3. Quit\n"; print "Enter your choice: "; my $choice = <STDIN>; chomp $choice; given ($choice) { when (1) { play_guessing_game() } when (2) { play_another_game() } when (3) { last } # Exit the loop for quitting default { print "Invalid choice. Try again.\n" } } }