Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Lead characters on Perl variable names

Perl variable names mostly start with a special character:

$ for a scalar variable - that's a variable that can hold an integer, a float, a string, a reference, or a compiled regular expression (that last not being terribly common).

@ for a list - that's an ordered collection of scalars, indexed from position "0" upwards. Since each individual member is a scalar, it can be an integer, a float ... etc, and it is referenced as an individual variable by a $ in front, and square brackets around the elements.

% for a hash - that's an unordered collection of scalars, indexed by other scalars. A good parallel here is a database table with two columns - one of which is a unique key and the other a value associated with it. Since members are scalars, they are referenced with a $ in front, and curly braces around the individual elements indicate members

& for a code variable - i.e. a "sub". You'll find that the & character is often omitted as a pair of round brackets - with or without a parameter - also indicate "run this code".

NO SPECIAL CHARACTER IN FRONT for a file handle. Conventionally written all in capitals ("I know what I am doing - I really DO intend to use a file handle here!") but they could be written lower case. File handles are used in limited places - open, close, seek, read <xxx>, etc and are really a sort of lightweight object, with handles also being connected to pipes, sockets, etc - they are (in reality) stream handles.

* for a typeglob - a use of "one of each of the above". Again, not very commonly used - the major use is for providing a mechanism for handling an 'array' of files; you can't do this directly with a list as a list is of scalars, and a file handles is not a scalar.

Here's some code including training examples of each of those, and also showing you context - how a list (for example) can be taken as a series of elements, a string made up of all the elements of a list, or the length of a list depending on the code surrounding the reference you make to it. These examples from the Perl for Larger Projects course I am running at present - day one's revision of Perl Basics.

$abc = "jashjkshjkhjkerhjkerhkert";
@stuff = ("Sage","Onion","feathers","foam rubber",66);
%henry = ("August", "Barbados", "September", "New Zealand",
  "October", "Tallin", "November", "Hull", "July",
  "Hawaii", "June", "Penge", "May", "Cape Town");
 
# Context Revision ---------------------
 
$"=", "; # Set separator to ', '
 
print @stuff,"\n"; # list context
print scalar(@stuff),"\n"; # Scalar context - no. els
print @stuff + 0 ,"\n"; # Scalar context - no. els
print "@stuff","\n"; # double quotish context
print (join(", ",@stuff),"\n"); # MUCH More maintainable.
 
print "$stuff[1] - @stuff[1] - \n"; # First good, second baaaaad
print @stuff[0,3,4],"\n"; # list slice
print @stuff[1..3,-1,2],"\n"; # list slice - any ole order!
print $#stuff,"\n"; # Index number of top element (i.e. length -1)
 
# String handling revision ----------------
 
$him = "John";
$message = "\"Don't do that\" $him said\n";
$message2 = qq("(please) Don't do that" $him said\n);
$message3 = <<"zSfs";
"I would rather you didn't to that " explained $him
"since there is already another dog in the bath and
the two of them will splash fearsomcley"
zSfs
print $message;
print $message2;
print $message3;
 
# " - a string operator
# ' - a literal string
# ` - run at shell / command level and save result
 
# Hash Revision ------------------------------
 
print %henry,"\n";
print (join(", ",%henry),"\n");
for ${widget} (keys(%henry)) {
  print "$widget and we visit $henry{$widget}\n";
  }
 
@movals = sort keys %henry;
print "@movals\n";
 
sub bylength {
  length($b) - length($a);
  }
 
@movals = sort bylength keys %henry;
print "@movals\n";
 
# Special variable types -------------------------
 
$a = "Henry";
$b = "Whillemena Smith";
$dx = &bylength; # Run a code variable
print "$dx\n";
 
$stuff = "Goods, Possessions, Chattels";
*nuvver = *stuff; # package deal - @stuff, %stuff, $stuff, &stuff, stuff
print "@nuvver\n";
print "$nuvver\n";


If this is a useful revision, then you're set for the Perl for Larger Projects or Using Perl on the Web courses. If you're thinking "I need to learn this", then please come on our Perl Programming course. There are varients for those who have programmed before, and for complete novices.

To complete my example, here is the output from running that program:

Dorothy-2:pl grahamellis$ perl fred
SageOnionfeathersfoam rubber66
5
5
Sage, Onion, feathers, foam rubber, 66
Sage, Onion, feathers, foam rubber, 66
Onion - Onion -
Sagefoam rubber66
Onionfeathersfoam rubber66feathers
4
"Don't do that" John said
"(please) Don't do that" John said
"I would rather you didn't to that " explained John
"since there is already another dog in the bath and
the two of them will splash fearsomcley"
SeptemberNew ZealandJunePengeJulyHawaiiMayCape
TownNovemberHullOctoberTallinAugustBarbados
September, New Zealand, June, Penge, July, Hawaii, May,
Cape Town, November, Hull, October, Tallin, August, Barbados
September and we visit New Zealand
June and we visit Penge
July and we visit Hawaii
May and we visit Cape Town
November and we visit Hull
October and we visit Tallin
August and we visit Barbados
August, July, June, May, November, October, September
September, November, October, August, June, July, May
11
Sage, Onion, feathers, foam rubber, 66
Goods, Possessions, Chattels
Dorothy-2:pl grahamellis$

(written 2009-08-24, updated 2009-08-26)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P050 - Perl - General
  [4301] Perl - still a very effective language indeed for extracting and reporting - (2014-09-20)
  [4296] Polishing the Perl courses - updated training - (2014-09-17)
  [3911] How well do you know Perl and / or Python? - (2012-11-04)
  [3902] Shell - Grep - Sed - Awk - Perl - Python - which to use when? - (2012-10-22)
  [3823] Know Python or PHP? Want to learn Perl too? - (2012-07-31)
  [3407] Perl - a quick reminder and revision. Test yourself! - (2011-08-26)
  [3332] DNA to Amino Acid - a sample Perl script - (2011-06-24)
  [3322] How much has Perl (and other languages) changed? - (2011-06-10)
  [3093] How many toilet rolls - hotel inventory and useage - (2010-12-18)
  [2971] Should the public sector compete with businesses? and other deep questions - (2010-09-26)
  [2825] Perl course - is it tailored to Linux, or Microsoft Windows? - (2010-06-25)
  [2783] The Perl Survey - (2010-05-27)
  [2736] Perl Course FAQ - (2010-04-23)
  [2504] Learning to program in ... - (2009-11-15)
  [2242] So what is this thing called Perl that I keep harping on about? - (2009-06-15)
  [2228] Where do I start when writing a program? - (2009-06-11)
  [1897] Keeping on an even keel - (2008-11-21)
  [1750] Glorious (?) 12th August - what a Pe(a)rl! - (2008-08-12)
  [743] How to debug a Perl program - (2006-06-04)
  [400] New in the shops - (2005-08-01)
  [116] The next generation of programmer - (2004-11-13)

P301 - Variables in Perl
  [4608] Introspecion in Perl 6 - (2016-01-02)
  [4398] Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl - (2015-01-18)
  [3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
  [3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [2877] Further more advanced Perl examples - (2010-07-19)
  [2241] Perl references - $$var and \$var notations - (2009-06-15)
  [1946] Variable Types in Perl - (2008-12-15)
  [1581] What is an lvalue? (Perl, C) - (2008-03-18)
  [975] Answering ALL the delegate's Perl questions - (2006-12-09)


Back to
Translation from Ghanaian to English
Previous and next
or
Horse's mouth home
Forward to
Designing your data structures for a robust Perl application
Some other Articles
Handling XML in Perl - introduction and early examples
Wiltshire / Melksham Weddings - guest accommodation
Long job - progress bar techniques (Perl)
Designing your data structures for a robust Perl application
Lead characters on Perl variable names
Translation from Ghanaian to English
Public Transport from (and to) Melksham on Sundays
Quiet summer days? I think not!
C++, Python, and other training - do we use an IDE
Using a cache for efficiency. Python and PHP examples
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2374_Lea ... names.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat