Variables

Varible names are of standard convention: starts with a-z, or underscore and can contain a-z, 0-9, and underscore.

Variables are prefixed by $, #, %, or * according to usage.

  • $value: is a scalar (simple, single-value) like a number, string, or reference.
  • @array: is an array (list of single-values). When accessing a single value from an array, code it with the $ prefix: $array[0] with the number in square brackets indicating which value to use (0 is first value, 1 is the second as so on).
  • %hash: is a hash or associative array mapping a unique key to a value. When accessing a single value from the hash, code it with the $ prefix: $hash{key} where the key is a scalar value variable, expression, or constant.
  • *glob: References the “glob” type, which is rather advances usage, and you should not use it much.

Data Types in Perl:

  • Scalar: $var = 123;
  • Arrays: @array = (1, 2, 3);
  • Hashes (Associative Arrays): %hash = ( key⇒value, ...);
  • Globs (gurus only): don’t get me started
  • References
    • Scalar References: $ref = \$var;
    • Array References: $ref = \@array; $ref=[1,2,3];
    • Hash References: $ref = \%hash; $ref={key⇒value,...}
    • Code References: $ref = sub { ++$count; }
    • Object References: later!!

These types will be explained further.

 
programming/perl/variables.txt · Last modified: 2005/07/15 10:14 by allen