iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 17, 2008
Learn HTML forms
About
 
May 16, 2008
Who Are Your Top Friends on Facebook?
About
 
May 15, 2008
Reader Question - Would you host your client's work on your website?
About
 
May 15, 2008
How to Create an Ajax Autocomplete Text Field: Part 6
WebReference.com
 
May 14, 2008
Poll: Are the browser safe colors still needed?
About
 
May 14, 2008
Google Doctype launched
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /CGI and Perl

Scalars 

  Views:    6716
  Votes:    1
by Spyder Co 4/22/05 Rating: 

Synopsis:

Learn about the most fundamental variable type used in Perl.
Pages: 
The Article

Scalars are the most fundamental variables or storage types used in Perl.  Because of this, these are used more often than all the rest combined.  Scalars hold on piece of information; it can hold numbers, a word or a phrase.  A scalar can contain a full page of text if that's what you wanted to do, but it only ones one piece of information.

$scalar = "";

Scalars are defined using the symbol $ before it's name.  For example $name, $state, $color, etc. are all valid names.  Variable names should always start with a letter but you may use numbers and underscores inside them ($name3 or $pizza_cheese).

Setting up your first scalar

To initialize any variable in perl, we use my before we call it's name.  We only my the variable the first time it is used within the script, each additional time you just call $scalar.

my $name = "sulfericacid";

Here we are making the new scalar $name.  "name" is the actual name of our variable, again this can be pretty much anything as long as it starts with a letter.  We are using my before the variable name since it wasn't made before this and we are assigning the value "sulfericacid" to $name.

Instead of having to type out "sulfericacid" in your script wherever you want to place it, you can just use $name instead.  The reason behind variables is to store re-usable content so you don't constantly have to type it out.

If you wanted to setup a scalar containing your favorite quote, you would do something like:

my $quote = "To know me, know yourself";

Now $quote contains our favorite quote so we don't have to continually type it out whenver we want to use it within our script.

Printing a scalar

Of the three variables you'll be learning, scalars are the easiest in terms of creation and printing.

print $scalar;

In Perl, we use the command 'print' to display something to the screen.  It's the same thing as 'echo' in the PHP language.  You call print $scalar;  and whatever was contained within the scalar variable will be printed to the screen.

For example, we'll create a scalar and store our pet's name inside and print it to screen:

my $pet = "molly";
print $pet;

We setup $pet and printed it to the screen.  You should now see "molly" on the screen if you run the script.

Deleting a scalar value

Generally you don't wory about deleting the contents of a scalar.  If you don't want to print it to screen or manipulate it into the rest of your program, just don't use the variable anymore.  If you truly wanted to delete it, you could assign it's value to a null value "".

$pet = "";

Now pet doesn't contain anything, it's just empty.

Copying a scalar

If you had a variable setup and you wanted to make a different copy of it, you can set the scalar value to another scalar itself.

my $house = "red";
my $car = "$house";

We made the scalar $house with the value of red.  We then made a new variable $car and assigned it to the value of $house.  So if you were print $car; , you would get "red" on the screen.

Doing this will put all the contents of one variable into the other, incase you wanted to make a backup.

Pages: 

Similar/related articles:


 
  Sponsors