Using functions within Perl

As with all languages, the ability to encapsulate a code within functions can make the code more readable and ultimately results in easier to manage codes, with less number of lines. Unlike bash, the functions in Perl can be defined after they are referenced in the code and we often choose to define the functions at the end of the script.

Prompt for user input

We have seen the use of command-line arguments in Perl; now, let's take a look at prompting for user input. This becomes a great way to encapsulate the code to execute and store the prompt within a function. First of all, we will look at a simple script that prompts for the username and then we will modify it to include the function. We will create the $HOME/bin/prompt.pl file to read, as shown in the following code example:

#!/usr/bin/perl
my $name;
print("Enter your name: ");
chomp( $name = <STDIN> );
print("Hello $name
");

In line 2, we have declared the variable using my. The keyword my defines the variable with a local scope. In other words, it is local to the block of code that it is created within. As this has been created in the main body of the script, the variable is available to the entire script. The line declares the variable but we do not set the value at this time. Perl does not force you to declare the variables, but it is a good idea and a great practice. In fact, we can tell Perl to enforce this with the use of the use strict; line. We can implement this, as shown in the following code block:

#!/usr/bin/perl
use strict;
my $name;
print("Enter your name: ");
chomp( $name = <STDIN> );
print("Hello $name
");

With this in place, we are forced to declare the variables and the code will fail if they are not. The idea behind this is to help troubleshooting by identifying misspelled variables later in the code. Try deleting the line starting with my and re-executing the code; it will fail. Similarly, we can make use of the use warnings; line, to warn us if we have used a variable only once.

We prompt for the user name and do not use a newline here. We want the prompt to be on the same line with the one the user will enter the data into. The function chomp is great isn't it? This function will remove or chomp the newline from the input that we submit. We will need to use the Enter key to submit the data and chomp removes the newline for us.

Creating the function

We are currently only prompting for a user name, so we need just one prompt but we could easily ask for a first name and last name. Rather than writing the code for the prompt each time, we can create a function. These are defined using the keyword sub, as we can see in the following code:

#!/usr/bin/perl
use strict;
my $name = prompt_user("Enter a name: ");
print("Hello $name
");

sub prompt_user () {
   my $n;
   print($_[0]);
   chomp( $n = <STDIN> );
   return($n);
}

The prompt_user function takes a single argument, which will become the message to display with the prompt. For the reference to the argument, we use the system array @_ and index 0. This is written as $_[0]. If we remember, arrays are multi-valued and each entry in the array is a scalar variable. Within the function, we use the function return to send the value that the user has set back to the calling code. We can see that the main code block is simpler now with the code for the prompt being abstracted into a function. When we look at this, we may think that it took a lot of work, but when we look at adding it in a prompt for a first name and last name, it is now much simpler.

Functions are good things to use and hopefully the following code will help you see this:

#!/usr/bin/perl
use strict;
my $fname = prompt_user("Enter a first name: ");
my $lname = prompt_user("Enter a last name: ");

print("Hello $fname $lname
");

sub prompt_user () {
   my $n;
   print($_[0]);
   chomp( $n = <STDIN> );
   return($n);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.178.73