Receiving User Data: < STDIN > and chomp

In this tutorial of our Perl course, we will learn how to receive user keyboard data through the <STDIN> operator and the chomp function.

Perl programming interactivity

So far, our scripts have always worked a certain way, always repeating the same, and always resulting in the same things (printing information on the screen).

But is this what happens in most of the programs we use?
Let's think a little.

If you click on a song, your player plays it.
If you click on another one, it will run another song.

If you join your social network, some information appears.
If someone else logs in, different things will appear.

If you type a certain URL, you go to a website.
If you type another, go to another site.

That is, programs, scripts, and systems react differently depending on the information they receive from the user. There is an interactivity, the machine does a certain thing if you do a certain action.

Receiving User Information in Perl: <STDIN>

Let's have the person who will use our Perl scripts provide data, information to the program.

First, let's take this information from the user's keyboard (other ways can be used, such as taking data from an internet form, a file from your machine, a particular location on the screen that the person clicked on, etc etc).

Let's do this through the <STDIN> operator.
To store a keyboard entry and store it in the $ my_variable variable, we do:

  • $ my_variable = <STDIN>;


Run the script through the command terminal.
Note that first it displays the first print of the script, then nothing happens in the program.

This is because Perl has arrived at <STDIN> (standard input) and it is waiting for the user to type in something and press ENTER.

Enter "Progressive Perl Course".
Then it prints the variable on the screen, which stores what you typed, see the result:
How to use <STDIN> in Perl

Perl's chomp function

Let's create a script that asks the user for data, a number, for example, and then prints it on the screen:
#!/usr/bin/perl

print "Type a number: ";
$num = <STDIN>;
print "You wrote: $num";
Note a curious thing, after printing the number on the screen, there is a line break:
Perl standard input <STDIN>

But there is no \n character in the script, so why?

What happens after we enter the number?
We hit enter.

So the $num variable doesn't just have "2112" stored.
It actually has "2112\n" with the line break, because you hit enter.

However, we don't always want this thing in the end. If we are going to work with numbers, for example, we just want 2112 and no \ n, do you agree?

So, that's where the chomp() function comes in.
Just put the variable inside the parentheses and this function will take \n.

Here's how the code looks using the chomp () function:
#!/usr/bin/perl

print "Type a number: ";
$num = <STDIN>;
chomp($num);
print "You wrote: $num";

And the result:
How to program in Perl

Now yes, we only have the number stored in the $num variable!
We can even take an even bigger tug at our code:
#!/usr/bin/perl

print "Type a number: ";
chomp($num = <STDIN>);
print "You wrote: $num";

Damn! This Perl is amazing!

Exercise

Download and install Visual Studio Code.
Create a file "progerssiveperl.pl", put the scripts of this tutorial, go to "Terminal -> Run Active File", did your code run ok?

No comments:

Post a Comment