13aal
13aal

Reputation: 1674

Writing Ruby Script In Perl

I'm writing a simple script in three different languages, Python, Ruby, and Perl. I'm extremely new to Perl, but really want to learn it. My problem being, I don't know how to write a method in Perl like in Ruby. I'm pretty sure that a method is called a function in Perl, but I'm not exactly sure..

Here's what I'm writing in the three languages: Ruby(all I have so far)

=begin 
Test program to choose language
Ruby
creator Me
=end

def welcome
    choices = %w(Perl Python Ruby)
    lang = 3
    puts "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"
    puts "There are", lang, "languages to choose from please choose one:"
    print choices
    print ">\t"
    input = gets.chomp
    if input =~ /perl/i
        puts "You have chosen Perl!"
    elsif input =~ /python/i
        puts "You have chosen Python!"
    else
        puts "You're already writing in Ruby!! Let me choose for you:"
        print "#{choices.sample}\n"
    end
end
welcome

As you can see this is a very simple script, I just feel as though writing it in three different languages will help me choose which one I want to learn next( I already know Ruby ).

Can someone explain to me how to write a method in Perl please? I've googled it but I can't seem to get anywhere with "method in Perl" It'd be greatly appreciated thank you ahead of time.

Upvotes: 0

Views: 127

Answers (3)

glenn jackman
glenn jackman

Reputation: 246942

With perl, variables have "sigils" to determine their "type":

my $scalar = "a scalar value";
my @array = ('a', 'list', 'of', 'scalar', 'values');
my %hash = (key1 => "value1", key2 => "value2");

See perldoc perldata.

Starting from perl version 5.10, there is an optional say command available. It is the equivalent to ruby's puts.

use feature qw(say);
say "I get a newline automatically.";

See perldoc feature.

And to find the length of an array

my $num = scalar @choices;   # or,
my $num = @choices;

A random element of an array:

my $rand_elem = $choices[rand @choices];

Upvotes: 1

simbabque
simbabque

Reputation: 54333

In Perl, not everything is automatically an object, so there are no methods per se. Functions are called subroutines, or sub. You can create one with the sub keyword.

sub foo {
  # ...
}

You call it by using the identifier. In our example that's:

foo();

For more details, please see the various ressources linked in the [tag wiki]1, or check perlsub.

Upvotes: 0

Quentin
Quentin

Reputation: 943686

You don't seem to be doing anything OO in that code, so a method seems like overkill. It would be more usual to use a simple subroutine.

sub welcome {
    ...
}
welcome();

If you really wanted to use a method, then perlootut is the classic method. A lot of people write OO code using Moose these days though.

It still comes down to writing a sub though, just one placed in the middle of a package definition instead of a simple script.

Upvotes: 3

Related Questions