Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
References and variables in Perl - a summary

Posted by admin (Graham Ellis), 14 February 2004
You may find this "revision table" useful if you've got a complex looking series of {}s, @s %s and $s in a piece of code you've been given to maintain!

1. Perl variables are dynamically created as your program runs.
  $abc         a scalar - holds a single number, string or reference
  @abc         a list - holds a collection of scalars indexed by position number
  %abc         a hash - holds a collection of scalars indexed by an scalar key of programmer's choice
  abc          a file handle - used for a set of buffers / interface to file system
  &abc         code - holds a named block of code - i.e. a sub
  *abc         a typeglob - a scalar, a hash, a list, a code and a file handle all of the same name

2. If you're referrring to an individual scalar member of a collection, you use the $ prefix and you use
  square bracket subscript           to indicate a member of a list
  curly brace subscript              to indicate a member of a hash

3. If you preceed a variable name, you can reference and dereference. Thus:
  \            means "A reference to"  or "the address of" if you prefer
  $            means "contents of" or "refers to"
  ->           an alternative notation for $
               example -  $$abc[3] and $abc->[3] mean the same thing
 
4. You can create anonymous lists and hashes
  $abc = {"England", "London", "Wales","Cardiff"}    creates an anonymous hash with two pairs
  $abc = {England => "London", Wales => "Cardiff"}   alternative notation
  $abc = ["England", "London", "Wales","Cardiff"]    creates an anonymous list with 4 members
  $abc = ("England", "London", "Wales","Cardiff")    probably WRONG - $abc takes the value "4".

5. If you refer to a list in a scalar context, you'll get the number of elements in the list.
If you refer to $#listname, you'll get the index number of the last element in the list.

6. You can also use {} to delimit a variable name - thus
  "$abkgs"     means a string containing the contents of the variable $abkgs
  "${ab}kgs"   means a string containing the contents of the variable $ab followed by "kgs"

7. You are limited only by your own ingenuity as to how complex a structure / expression you can make up using the above; I suggest that you use an Object Oriented design is you're looking at a really complex problem as this helps divide the code and complexity down into a series of different parts each of which can be developed and tested before you move on to the next stage.

Posted by admin (Graham Ellis), 14 February 2004
Here's some sample (demo) code using most of the things I described in the previous post - can you work them out??

Code:
$var = "Hello";
$itsat = \$var;     # reference to a scalar

print $var,"\n";
print $itsat ,"\n";
print $$itsat ,"\n\n";   # contents of

@stuff = ("tom","dick","arry");
$itsat = \@stuff;     # reference to a list

print @stuff,"\n";      # no separator
print "@stuff\n";       # space separator
print @stuff+0,"\n\n";  # scalar content - length of list

print $itsat,"\n";
print @$itsat,"\n";     # list referenced by scalar
print @{${itsat}},"\n\n";       # list referenced by scalar
print $$itsat[2],"\n";  # element of list pointed to by a scalar
print $itsat->[2],"\n"; # element of list pointed to by a scalar
print $itsat->[-1],"\n\n";      # element of list pointed to by a scalar

%table = ("Chris" , "S&Kpie", "Paul" , "Burger");
%table = (Chris => "S&Kpie", Paul => "Burger");  # alternative
$itsat = \%table;

print $itsat,"\n";
print %$itsat,"\n";     # hash referenced by scalar
print %{${itsat}},"\n\n";       # hash referenced by scalar

print $$itsat{"Chris"},"\n";    # element of list pointed to by a scalar
print $itsat->{"Chris"},"\n";   # element of list pointed to by a scalar
print $itsat->{"Chris"},"\n\n"; # element of list pointed to by a scalar


$itsat = {"Carine","Pastie","Pali","Haddock"};  # ref to an anonymous hash
print $itsat,"\n";
print %$itsat,"\n";     # hash referenced by scalar
print %{${itsat}},"\n\n";       # hash referenced by scalar

print $itsat->{"Carine"},"\n";
print $$itsat{"Carine"},"\n";
print ${${itsat}}{"Carine"},"\n";

$itsat = ["Lancashire","Yourkshire","Elsewhere"]; # ref to an anon list
               # ( ) would give "3"!

print $itsat,"\n";
print @$itsat,"\n";     # list referenced by scalar
print @{${itsat}},"\n\n";       # list referenced by scalar
print $$itsat[2],"\n";  # element of list pointed to by a scalar
print $itsat->[2],"\n"; # element of list pointed to by a scalar
print $itsat->[-1],"\n\n";      # element of list pointed to by a scalar



$yikes = [{"Tom" => "Plumber", "Dick" => "Copper"},
       {Carine => "Programmer", "Paul" => "Programmer"},
       {Pali => "Hard Worker"}];

print "$yikes\n";

foreach $el (@$yikes) {
       print "$el\n";
               foreach $k (keys %$el) {
                       print ("$k $$el{$k}\n");
               }
       }




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho