Perl: Releasing Your Inner Textuality - More Printing Information (
Page 4 of 4 )
Another thing to keep in mind is that you must use a backslash to print a $ symbol. Without it, Perl will try to interpret it as a variable. Behold:
#!/usr/bin/perl
print "I have $15.00 in my pocket boyee!";
If you try to run that program you will get an odd result:
I have .00 in my pocket boyee!
Very odd indeed. Since we aren't trying to print a variable, the proper way to write this code would be:
#!/usr/bin/perl
print "I have \$15.00 in my pocket boyee!";
Which would give us the correct print out of:
I have $15.00 in my pocket boyee!
Similarly, you cannot print out an @ symbol either. The following will not work:
#!/usr/bin/perl
print "My email address is james@james.com.";
If you used this code it would print:
My email address is james.com.
To fix this, we again use our good old buddy the backslash:
#!/usr/bin/perl
print "My email address is james\@james.com.";
Which prints out:
My email address is james@james.com .
And lastly, we can also use escape characters and special characters on our variables. Here is an example:
#!/usr/bin/perl
$color="black";
$number="eight";
print "\n\nPlease find my favorite color and number below:\n\n";
print "\tMy favorite color:\t\t\t$color\n";
print "\tMy favorite number is:\t\t\t$number\n\n";
This will print out our text and variables so that they appear like this on the user's screen:
Please find my favorite color and number below"
My favorite color: black
My favorite number is: eight
Well that's all the time we have for this exciting episode. Join us next time when we continue this intriguing, gut wrenching discussion on Perl text manipulation. I look forward to seeing you.
Till then...