Writing on the screen

February 11, 2009 · Posted in Perl

Well, one more post in the series "How to program in Perl" which is likely to become weekly, every Wednesday with a brand new post for you. Try to be as regular social gatherings of rio.pm , already a few years occur every second Thursday of the month. But back to the mini-tutorial ... Today we will talk about how to print things on the screen using perl.

One of the easiest ways to print something on screen is by using the print command, see the most classic example of using this command:

  ; print "Hello World"; 

And what does that thing there is?! There seems kind of obvious? This prints Hello World.

Not that this business is easy right?!

Well, there is something simpler, if you want it to print Hello World, you only need to send: print Hello World.

Yeah, okay ... That ... But I understand why the quotation marks (")?

Well, it tells perl that Hello World is a string, a text. If I not to put the quotes, perl would find that Hello and World are commands (functions). But do not hesitate to these functions, this code would error.

The print does not accept strings ... it only accepts a list of "things":

  , " " , "números: " , 1 , 2 , 3 , 4 ; print "Hello", "", "numbers", 1, 2, 3, 4; 

What does this do?! This prints: the string Hello, then space, then the string number: (with space at the end) and then 1, 2, 3, 4. How is it printed? Thus:

Hello numbers: 1234

It prints each of the items from the list without tabs. Let's see another example?

  , 1 ... 10 ; print "Numbers 1-10:" 1 ... 10; 

This prints:

Numbers 1-10: 12345678910

One more example:

  15 ; $ Number = 15;
 , $numero ; print "My number is equal to", $ number; 

Which prints:

My number is equal to 15

And it prints the same thing:

  15 ; $ Number = 15;
 ; print "My number is $ number"; 

Right? So we see that if I put a scalar variable between the quotes, the print will print the value of this variable. Interesting ... But if I do not want to print the value?! If for example I want to print: I have a variable called $ number?

One way to do this would be instead of using double quotes (") use single quotes ('):

Importantly, we're talking about quotes from vertical (as shown in snippets) and not those of French quotation marks (which appear in the rest of the text). This difference is due to the html of the blog to give a "tidy" in his own way, even using the codes entity .

  print 'I have a variable called $ number' 

Hum ... So to say that when using single quotes (') he does not interpret the value of variable, but exactly what is written. Beauty! But what if I want to print it?

The value of the variable $ number is 15

Well then it might be best to do so:

  numero é $numero" ; print "The value of the variable \ $ number is $ number"; 

Where'd you get that backslash (\ )?!?! What does this mean?

Well ... The backslash (backslash backslash or for the intimate) has a special role: She makes special things that are not special, and makes non-special things special.

??

Easy! Let me explain!

The character "$" is a special character within double quotes, he says that what is coming is a scalar variable (as discussed in previous post ) so if we use the backslash (\) before that character, it loses all his specialty, and becomes a single character, comes to mean simply "$". Another example is the character "(double quotes) that after another double quote means the closing of the first. Well, one example is worth a thousand words:

  numero é \" $numero \" " ; print "The value of the variable \ $ number is \" $ number \ "; 

Complex? Let's see:

We are sending a print string (we know it is because a string is quoted).

We open quotes, and write:

The variable value

Then we use a backslash (\) and a $, the backslash took the specialty of the $, then it is not a variable.

Once write is followed by the backslash (\) and double quote (") as the quote is no longer special (because the backslash) she is not closing ... the first quote is just another character to be printed.

After that comes the variable $ number without quotation marks and other specialty and special quote that closes the first.

Until not so complicated, right?

But what if not, the non-special character preceded by a backslash (\)? Let's see some examples:

The character "t" has nothing special, just a t ... Now, when putting a backslash in front, it completely changes the meaning. "\ T means tab, horizontal tab. Interesting, huh?

Another interesting example is the "n". If you are running these print s on your computer, you are noticing that none of them changed his line. Well, so far not print any characters of line break. This character is "\ n". Then we can write:

  O valor da variável \$ numero é \" $numero \" \n " ; print "\ t The value of the variable \ $ number is \" $ number \ "\ n"; 

What will print:

The tab value of the variable $ number is "15" quebra_de_linha

Okay ... I think it is good for this Wednesday ... I have to go to bed, because tomorrow I'll stay up late ... Tomorrow is the meeting of the Social rio.pm .

Remember, if you want something more advanced if you want to see some of my designs or want to play golf , visit the perl-e and have fun. I hope you guys there, and for the next 4th Friday with one more post about initiation in perl!

Comments

  • Angello Van Dahl
    Great post.! congratulations, I did not know this business of putting the backslash with "or $, help me a lot. VLW ...
blog comments powered by Disqus