JDT

 

FREE Downloads
Articles
Tutorials

 

Working with Numbers and Strings in Perl


In Perl, numbers and strings are called scalars. Scalars that do not change during the lifetime of a program are known as constants or literals. Scalars that do change are known as variables. Operators do something with scalars, for example, add together the contents of two variables.

Literals/Constants

There are two types of literal:

  • numeric literals, for example, 6, 8.5, 11, 17.678, and so on, and
  • string literals, for example, "hello world".

Numeric literals may be integers, for example, 6, floating point, for example, 12.5, or scientific notation, for example, 2e10. Strings are a sequence of characters, for example, goodbye cruel world.

Scalar Variables

Data that may change during the lifetime of a program is stored in variables, for example, $total. The dollar ($) sign is called a type identifier, and tells Perl that the variable contains scalar data.

Variable names can contain alphanumeric characters (a-z, A-Z, and 0-9) and the underscore character, and are case-sensitive.

Expressions and Operators

Perl programs are collections of expressions and statements that, by default, are executed in the order in which they appear in the program.

1. #!/usr/bin/perl -w
2. $radius=10;
3. $pi=3.1415927;
4. $area=$pi*($radius**2);
5. print $area;

The equals sign (=) is known as the assignment operator. It takes the value on its right-hand side, and puts it in the variable on the left. The star (*) is the multiplication operator. Two stars together (**) indicate 'to the power of'.

Another example:

1. $a="Goodbye";
2. $b="cruel world";
3. $c=$a.$b;
4. print $c;

The dot (.) concatenates the contents of variables $a and $b, and the result is put into variable $c.


Author: Backrubber
John Dixon Technology Ltd







Go back to Perl Tutorials home page

Go back to Tutorials home page



Earnings Tracker is John Dixon Technology's FREE open source accounting / bookkeeping software tool.

Earnings Tracker is aimed at contractors and freelancers, and enables you to perform many bookkeeping and accounting tasks, helping you to keep track of your company's earnings and outgoings.

The software is written in PHP and MySQL and is available to use for FREE online, or as a FREE download.

As it is written in open source software, you are free to modify the code yourself, enabling you to produce a customized version of Earnings Tracker that fits your own specification.

Earnings Tracker can also be used simply as a dividend, corporation tax, or VAT calculator.

Need free accounting software
 



JDT

Copyright Notice for John Dixon Technology Ltd

Privacy Statement

Terms & Conditions