Home arrow Perl Programming arrow Page 3 - Perl: Releasing Your Inner Textuality

Printing Variables - Perl

There are lots of way to express yourself, but with programming languages the simplest way to do that is usually text. This tutorial will walk you through ways to make text work for you in Perl. It's the first of a three-part series, and since (as usual) we have a lot of ground to cover, let's get started.

  1. Perl: Releasing Your Inner Textuality
  2. Special Characters Don't Need Helmets or Small Buses
  3. Printing Variables
  4. More Printing Information
By: James Payne
Rating: starstarstarstarstar / 1
February 25, 2008

print this article



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: 




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:




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:




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:








$Question="$a $b $c $d $e $f";

print $Question;

Which gives us:

  Hello how are you doing today?

>>> More Perl Programming Articles          >>> More By James Payne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: