Programs for different language learning - Perl 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.

See also Python

See also Ruby

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 = &lt;STDIN&gt;;
    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();
What you did well


use strict;, use warnings;, and my scoping
qw(list of words) instead of ('list', 'of', 'words')
Generally straightforward, readable Perl.


Suggestions


There is a hard-coded correspondence between my $lang = 3 and the fact that there are three choices.  My suggestion:

my $choices = @choices;


In Perl, $choices and @choices are different variables: the former is a scalar, and the latter is a list.  The assignment above coerces the list into a scalar, and it does so by taking the number of elements in the list.  (You used this technique in @choices[rand @choices].
Parentheses are optional for subroutine calls, and they are typically omitted for well-known functions such as print.
For maintainability, terminate the last print with a semicolon, even if you aren't required to.
As @glennjackman already told you, say would be better than print, as it also outputs a trailing newline.
Avoid super-long string literals by using a here document.

    print &lt;&lt;"INTRO";
Welcome to the test script…
There are $choices languages to choose from.
@choices
INTRO

Your logic is rather trusting: any input other than "Ruby" or "Python" is treated as if it were "Perl".

A better way:

use feature qw(say);
…

chomp(my $choice = &lt;STDIN&gt;);
if ($choice eq 'Perl') {
    say "You're already writing in Perl!! Let me choose for you:";
    say @choices[rand @choices];
} elsif (grep { $choice eq $_ } @choices) {
    say "You have chosen $choice!";
}

A possible outcome is "You're already writing in Perl!! Let me choose… Perl".  I consider that to be a logic bug.