
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
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
P301 - Variables in Perl [975] Answering ALL the delegate's Perl questions - (2006-12-09)
[1581] What is an lvalue? (Perl, C) - (2008-03-18)
[2241] Perl references - $$var and \$var notations - (2009-06-15)
[2374] Lead characters on Perl variable names - (2009-08-24)
[2877] Further more advanced Perl examples - (2010-07-19)
[2972] Some more advanced Perl examples from a recent course - (2010-09-27)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
[4398] Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl - (2015-01-18)
[4608] Introspecion in Perl 6 - (2016-01-02)
P202 - Perl Fundamentals [184] MTBF of coffee machines - (2005-01-20)
[748] Getting rid of variables after you have finished with them - (2006-06-06)
[1312] Some one line Perl tips and techniques - (2007-08-21)
[1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
[1726] Hot Courses - Perl - (2008-07-28)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3329] Perl from basics - (2011-06-20)
[3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
[3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
[3574] Perl functions such as chop change their input parameters - (2012-01-10)
[3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[4324] Learning to program - variables and constants - (2014-11-22)
Some other Articles
Copyright of Training Notes and Web SiteNuclear Physics comes to our web siteBlame CulturePerl substitute - the e modifierVariable Types in PerlSummary - Apache httpd build on LinuxForwarding session and cookie requests from httpd to TomcatPort and GlassesChristmas scenes and eventsServer - Service - Engine - Host, Tomcat