I've created a program for my own purposes in three different languages, Perl, Python, and Ruby. Each program will be posted in with their own tag but I will add links to them through on each post. The program itself is very simple, all it does is ask the user about three different languages which are saved into an array, it then prompts the user to choose a language that runs through an if statement, and if the user chooses the language they're writing in it then takes a random language from the array and outputs it.
use strict;
use warnings;
sub welcome {
my @choices = qw( Perl Python Ruby );
my $lang = 3;
print("Welcome, to the test script, this will test what language you would like to learn.. In order to find out these choices, write this same definition in all three different languages\n");
print("There are $lang, languages to chose from please pick one:\n");
print "@choices\n";
my $choice = <STDIN>;
chomp $choice;
if ($choice eq "Ruby") {
print("You have chosen Ruby!\n");
}
elsif ($choice eq "Python") {
print("You have chosen Python!\n");
}
else {
print("You're already writing in Perl!! Let me choose for you:\n");
my $rand_elm = @choices[rand @choices];
print "$rand_elm\n"
}
}
welcome();
Perl took me the longest out of all the programs I wrote, this was my first ever Perl script. With all the ;
and the random symbols I had to use to make variables and arrays, it was defiantly hard. However I really enjoyed writing this in Perl, it was something different. Perl maxed out as the longest program at a total of 30 lines. At one point I kept getting an NameError
that was due to me missing a ;
at the chomp function. All in all I think Perl is definitely a great language to learn.
I also have some questions about Perl:
- Is there any better syntax that I should be using here in order to shorten the program?
- Does Perl have an equivalent to Ruby's
puts
? - Why is it easier to use a
sub
instead of a function?
example:
welcome();
# instead of
sub welcome();
def
to define a method; in Perl you usesub
to define a subroutine. Same idea, different keyword. \$\endgroup\$