CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in
Add ListingModify ListingTell A FriendLink to TPASubscribeNew ListingsCool ListingsTop RatedRandom Link
Newest Reviews
  • review
  • hagen software
  • NOT GPL!
  • Hagan Software
  • Wasted Time with ...
  • poor pre-sale sup...
  • no response
  • rating the offer
  • Good Stuff
  • Good idea but use...


  •  
    Perl Archive : TLC : Programming : Perl : Intro to Perl: Scalar Variables
    Guide Search entire directory 
     

    Date Published: 1999-08-01

    Intro to Perl
    Main Page
    Part 1: Scalars
    Part 2: Arrays
    Part 3: Hashes
    Part 4: Subroutines
    Part 5: Putting it Together

    by D. Jasmine Merced
    Tintagel Net Solutions Group, Inc.

    Scalars

    A variable, by definition is "Likely to change or vary; subject to variation; changeable". A scalar variable is basically a variable that allows you to define one piece of data for later use. For example:

    $sendtomail = 'admin@perlarchive.com';

    Here, we defined $sendtomail to admin@perlarchive.com. After defining the scalar, the programmer can use $sendtomail anywhere into their code -- and when the program is run, wherever $sendtomail appears, admin@perlarchive.com will be inserted.

    You can easily identify a scalar variable in a program, because it always starts with a dollar sign (eg: $sendtomail) and has a definition (e.g.: = 'admin@perlarchive.com').

    Continuing with the above example, let's say the program sends an email to the email address defined in $sendtomail. The following line tells the program who the email is to.

    print MAIL "To: $sendtomail\n";

    Later in the program, the programmer can also use:

    print "Thank you for your input! Your message has been sent to $sendtomail";

    Variables, in essence allow you to set a definition once, and use it as many times as you need to.

    If you've installed perl programs before, you probably have already seen variables in program setup (configuration) section. These configuration settings are usually either in a file by itself, or at the top of the main program file.

    By having a group of configuration settings, the programmer makes it very easy to distribute the program to many different people.

    Defining and "Escaping" Scalars

    Defining scalars follows a specific format. The dollar sign immediately tells the program that it's a scalar. Immediately following the dollar sign is an alphanumeric string (never starting with a number), then an equals sign (=), then the definition, and last, the semi-colon (;). Put all together, it looks like:

    $sendtomail = 'admin@perlarchive.com';

    Notice that in my example, the admin@perlarchive.com is enclosed in single quotes ('admin@perlarchive.com'). Sometimes you'll see a scalar definition enclosed in double quotes ("admin\@perlarchive.com"). Wait a second, where did that backslash (\) come from? And at other times, you won't see quotes at all ($total = $subtotal * $taxrate). Uh oh… (There are others, too, but they're used for actual programming and beyond the scope of this article.)

    Because Perl is such a robust language, it takes into account the different types of data you want to store in your scalars. Let's start with text.

    Text is any alphanumeric string which can also include any special character (~!@#$%^&*()_). Because perl uses some of these special characters (such as the $ that goes in front of a scalar), we need to tell perl "Even though there's a special character here, don't do anything special with it - just display it!".

    3 of the most common special characters perl uses to define/retrieve data:

    $ tells perl it's a scalar
    @ tells perl that it's an array (in part 2)
    % tells perl that it's a hash (in part 3 Next issue)

    Now that we know this, we just need to know how to tell perl not to interpret these special characters. There are 2 ways:

    1. Enclose the scalar definition in single quotes. The single quotes tell perl to treat the string literally, and not to interpret it.
      $sendtomail = 'admin@perlarchive.com';
    2. If using double quotes, escape the offending characters with a backslash
      $sendtomail = "admin\@perlarchive.com";

    Well, what if you wanted to use a single quote (aka apostrophe ') inside a scalar definition that's enclosed in single quotes? Easy, just escape the apostrophe using a backslash.

    $blah = 'It\'s a perl scalar variable.';

    The single (or double) quotes are at the beginning and at the end of the definition to mark the start and end of the definition. If you do not escape the repeated quote ('It's a perl scalar variable.') or double quote ("He said "a what?""), perl will think you're done where the apostrophe (or double quote) appears. So what do you think will happen if perl thinks that

    $blah = 'It'

    is your complete scalar and

    s a perl scalar variable.';

    is just hanging out at the end?

    Ha! Crash and burn it will, and produce a lovely "Internal Server Error", a completely misleading error message which just means that perl didn't like something in the code.

    Numbers are easy. They typically don't even need to have any quotes around them, even when performing calculations.

    $month = 12;
    $total = $subtotal * tax;

    If, however, you wanted a full, formatted date (9/1/1999), it would need to be treated as "text". The slashes would simply tell perl to performs multiple divisions.

    Summary

    Scalar variables allow you to store one piece of data for use throughout the program. They always start with a $ and have a few strict guidelines to follow to properly define them.

    Next, we're going to discuss arrays...

     

    D. Jasmine Merced is the President/CEO of TNS Group, Inc. and the administrator of The Perl Archive. She also serves as a Director of the World Organization of Webmasters.

     
     


    About The Perl ArchiveLink Validation ProcessSearch Tips
    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives