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
 
Variable Types in Perl

Learning Perl in CambridgeIn Perl, you have "autovivification" where variables are created when they have a values set in them, without the need to declare them. Some authorities will tell you that they are also "autotyped" in that Perl knows what to store in them automatically too, and to some extent that's true ... but the leading character is also important in at least some of the type decisions.

$ - a scalar, to hold an integer, a float, a string, a reference or a regular expression
@ - a list, to hold an ordered (referenced by counter) collection of scalars
% - a hash, to hold an unordered (reference by scalar key) collection of scalars
& - code, to hold a sub or method
No initial letter - a file handle - to hold the structure through which a file is accessed
* - a typeglob, used occasionally to handle a grouping of one of each of the above

Earlier today, I put together an example that sets up one of each variable type ...

$abc = "text"; # Scalar variable - string, number, reference or regex
@def = (1,4.5,"hello",$abc,\$abc); # List variable - a number of scalars
%ghi = (Wales => "Rugby", England => "Cricket"); # Hash variable - keyed scalars
sub jkl {print "Hello Wurld\n"; } ; # Code variable - named piece of code
open (MNO,">demo.gunk"); # File handle variable - for file reference
*pqr = *abc; # Typeglob - one of each of the above


and then makes use of it, or (in the case of lists, hashes and typeglobs) the scalars within:

print "$pqr ... \n"; # Same as $abc
print MNO "This is filed\n"; # To file - not seen on output
print (jkl,"\n"); # Runs jkl code, also prints "1" - its return value
print $ghi{Wales},"\n"; # Printing from a hash - note curlies
print $def[1],"\n"; # Printing from a list - note squares
print $abc,"\n"; # Straightforeward printing of a scalar!


See Full source code of example - and here are the results:

Dorothy:plpw grahamellis$ perl vartypedemo
text ...
Hello Wurld
1
Rugby
4.5
text



Illustration - on a private Perl course run which I ran at a customer's offices in Cambridgeshire
(written 2008-12-15, updated 2010-06-23)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P202 - Perl Fundamentals
  [4324] Learning to program - variables and constants - (2014-11-22)
  [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
  [3574] Perl functions such as chop change their input parameters - (2012-01-10)
  [3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
  [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
  [3329] Perl from basics - (2011-06-20)
  [3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
  [3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
  [3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
  [2876] Different perl examples - some corners I rarely explore - (2010-07-18)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
  [1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
  [1726] Hot Courses - Perl - (2008-07-28)
  [1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
  [1312] Some one line Perl tips and techniques - (2007-08-21)
  [748] Getting rid of variables after you have finished with them - (2006-06-06)
  [184] MTBF of coffee machines - (2005-01-20)

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)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [2877] Further more advanced Perl examples - (2010-07-19)
  [2374] Lead characters on Perl variable names - (2009-08-24)
  [2241] Perl references - $$var and \$var notations - (2009-06-15)
  [1581] What is an lvalue? (Perl, C) - (2008-03-18)
  [975] Answering ALL the delegate's Perl questions - (2006-12-09)


Back to
Summary - Apache httpd build on Linux
Previous and next
or
Horse's mouth home
Forward to
Perl substitute - the e modifier
Some other Articles
Copyright of Training Notes and Web Site
Nuclear Physics comes to our web site
Blame Culture
Perl substitute - the e modifier
Variable Types in Perl
Summary - Apache httpd build on Linux
Forwarding session and cookie requests from httpd to Tomcat
Port and Glasses
Christmas scenes and events
Server - Service - Engine - Host, Tomcat
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/1946_Var ... -Perl.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat