Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Variable Types in Perl

In 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

(written 2008-12-15 23:37:02)

 
Associated topics are indexed under
P103 - Perl - Variables and Operations
  [2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
  [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)

P301 - Variables in Perl
  [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
2666 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 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., 2010: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 344596 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho