Printing Information on Screen: PRINT and SAY in Perl

In this Perl tutorial, we will learn how to display information on the command terminal using Perl's print and say functions.

Printing information on the command terminal

Printing, for us, is very reminiscent of making a print on a printer, right?
In fact, it is a broader term, means to display.

So when we talk about printing, we are simply showing some information, a text, a number or anything.

This is the first way we will learn about how to exchange information between the machine and the person using it. All commands and operations occur 'hidden' on your computer hardware, so we use some command that displays these results to us.

The most common ways of displaying information data are through the print and say functions in Perl, which simply show the information we want on the command terminal screen, let's learn how to use it?

print function in Perl

This function (command that does something specific) we already used in the tutorial on how to program the script Hello world! in Perl.

Just type the print command and type anything in double or single quotation marks and end the command with a semicolon;

Ready. What you typed in the quotation marks will appear on the screen.
These texts, in Perl, are called strings.

Test:
#!/usr/bin/perl
print "Progressive Perl tutorial";
The result will be:
Progressive Perl tutorial

But it got ugly, because it was 'pasted' with a command line text.

Line break character: \n

To solve this problem, just hit the enter symbol.
Enter symbol, in computing, is symbolized by: \n

Whenever Perl reads this command, it gives a jump, a line break.

Try it:
#!/usr/bin/perl
print "Progressive Perl tutorial \n";
The result is what we saw in the last tutorial, much cuter and more organized, isn't it?

And to display the result below:
Free, complete and online Perl tutorial

How would you program the Perl script?
You can do so much:
#!/usr/bin/perl
print "Welcome to: \n";
print "Progressive Perl \n";
Like all in one line, see:
#!/usr/bin/perl
print "Welcome to:\nProgressive Perl\n";

SAY function in Perl

Programmer is a lazy people. He likes to do everything as quickly, simply and economically as possible, the less work the better.

As if the print function was no longer simple enough, they invented the say function.

Change print by say and see the result:
#!/usr/bin/perl
use v5.010;
say "Progressive Perl tutorial";
Have you noticed?
It automatically gives the line break at the end, no need to use the line break \ n character.

Nice, isn't it?

PRINT exercises in Perl

1. Phrase on screen - Implement a program that writes the phrase "The first program we never forget!"

2. Etiquette - Design a program that writes your full name on the first line, your address on the second, and zip code and phone number on the third.

3. Lyrics - Make a program that displays on the screen a lyrics you like (forbidden lyrics by Justin Bieber).

4. Message - Write a message to a person you like. Implement a program that prints this message, prints it out, and sends it to this person. Say it was a virus that a hacker installed on your computer.

5. To the site - Make a program that shows on screen what you want to do using your Perl skills.

6. Square - Write a program that shows the following figure:

XXXXX
X   X
X   X
X   X
XXXXX

7. Gradebook - You were hired by a school to make the student report card system. As a first step, write a program that produces the following output:
Student          Grade
=========         =====
BRUCE              9.0  
STEVE              DEZ
DAVE               4.5    
NICKO              7.0

8. Large Letter - Design a program to produce the letter P of Progressive Perl. If it were ‘L’, would be like this:
L
L
L
LLLLL

9. Menu - Design a program that shows the following menu on the screen:
Customer base
0 - End
1 - Includes
2 - Alter
3 - Excludes
4 - Consultation
Option:

10. Pine - Implement a program that draws a "pine" on the screen, similar to the one below.

Enrich the design with other characters by simulating embellishments.
       X
      XXX
     XXXXX
    XXXXXXX
   XXXXXXXXX
  XXXXXXXXXXX
 XXXXXXXXXXXXX
XXXXXXXXXXXXXXX
       XX
       XX
      XXXX

Write your scripts in the comments!

No comments:

Post a Comment