Home arrow Perl Programming arrow Page 2 - Scalars: Building a Currency Converter

Variable Names - Perl

In this conclusion to a five-part series on scalars in Perl, we'll put everything we've learned together to build a currency converter. This article is excerpted from chapter two of the book Beginning Perl, written by James Lee (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. Scalars: Building a Currency Converter
  2. Variable Names
  3. Currency Converter
  4. The chomp() and chop() Functions
  5. The die() Function
By: Apress Publishing
Rating: starstarstarstarstar / 2
April 22, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

We’ve not really examined yet what the rules are regarding what we can call our variables. We know that scalar variables have to start with a dollar sign, but what next? The next character must be a letter (uppercase or lowercase) or an underscore, and after that, any combination of numbers, letters, and underscores is permissible.

Note that Perl’s variable names, like the rest of Perl, are case-sensitive, so$useris different from$User, and both are different from$USER.

The following are legal variable names:$I_am_a_long_variable_name,$simple,$box56,$__hidden,$B1.

The following are not legal variable names:$10c(doesn’t start with letter or underscore),$mail-alias(- is not allowed),$your name(spaces not allowed).

The Special Variable $_

There are certain variables, called special variables, which Perl provides internally that you either are not allowed to or do not want to overwrite. One which is allowed by the preceding rules is$_, a very special variable indeed.$_is the default variable that a lot of functions read from, write to, and operate upon if no other variable is given. We’ll see plenty of examples of it throughout the book. For a complete list of all the special variables that Perl uses and what they do, typeperldoc perlvarat the command line.

Variable Interpolation

We said earlier that double-quoted strings interpolate variables. What does this mean? Well, if you mention a variable, say $name, in the middle of a double-quoted string, you get the value of the variable, rather than the actual characters. As an example, see what Perl does to this:

#!/usr/bin/perl -w
# varint1.pl
use strict;

my $name = "fred";
print "My name is $name\n";

This produces

$ perl varint1.pl
My name is fred
$

Perl interpolates the value of$nameinto the string. Note that this doesn’t happen with single-quoted strings, just like escape sequence interpolation:

#!/usr/bin/perl -w
# varint2.pl

use strict;
my $name = "fred";
print 'My name is $name\n';

Here we get

$ perl varint2.pl
My name is $name\n$

Notice that the system prompt is printed at the end of that line because\nis not a newline character within the single quotes. This doesn’t just happen in things we print, it happens every time we construct a string:

#!/usr/bin/perl -w
# varint3.pl

use strict;

my $name = "fred";
my $salutation = "Dear $name,";
print $salutation, "\n";

This gives us

$ perl varint3.pl
Dear fred,
$

This has exactly the same effect as

my $salutation = "Dear " . $name . ",";

but is more concise and easier to understand.

If you need to place text immediately after the variable, you can use curly braces to delimit the name of the variable. Take this example:

#!/usr/bin/perl -w
# varint4.pl

use strict;

my $times = 8;
print "This is the $timesth time.\n";

This is syntactically incorrect, because Perl looks for a variable$timesthwhich hasn’t been declared. In this case, we have to change the last line by wrapping the variable name in curly braces to this:

print "This is the ${times}th time.\n";

Now we get the right result:

$ perl varint4.pl
This is the 8th time.
$



 
 
>>> More Perl Programming Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- 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: