Perl: Releasing Your Inner Textuality - Printing Variables (Page 3 of 4 )
We've learned in the past to work with variables, and I know this is a bit redundant, but keep in mind that this article is more of a cheat sheet for different ways of manipulating text.
We store text in variables and print it like so:
!/usr/bin/perl
$a='Apple';
$b='Jacks';
print "The best cereal in the world is $a $b.";
This prints out:
The best cereal in the world is Apple Jacks.
One note of warning: in the previous section we talked about using single and double quotes together. This method does not work the same when dealing with variables. Observe the following:
#!/usr/bin/perl
$a='apple';
$b='jacks';
print 'The best cereal in the world is $a $b.';
This will print out:
The best cereal in the world is $a $b.
Which of course is not what we were hoping for.
To get it to print the values inside the variables, we have to use double quotes, like so:
#!/usr/bin/perl
$a='Apple';
$b='Jacks';
print "The best cereal in the world is $a $b.";
Which results in the proper print-out (and a very true statement):
The best cereal in the world is Apple Jacks.
The reason why this occurs is simple enough. When Perl sees a single quotation mark, it does not interpret; when it sees double quotation marks, it does.
Note that you can store an unlimited amount of variables within your double quoted string:
#!/usr/bin/perl
$a="Hello";
$b="how";
$c="are";
$d="you";
$e="doing";
$f="today?";
$Question="$a $b $c $d $e $f";
print $Question;
Which gives us:
Hello how are you doing today?
Next: More Printing Information >>
More Perl Articles
More By James Payne