First ... Back ... Next ... Last ... (Notes home)

Perl Data Types

  • man perlintro for an overview
  • Perl is not a strongly typed language. Generally, you do not need to worry about "classical" data types, since Perl will interpret the type correctly from context. However, you do need to distinguish between list and scalar types, and the different types of arrays.
  • Fundamental type: scalar. This is a variable that can only have one value.
  • Fundamental type: list. This is a variable that can contain a list of values. It includes arrays.
  • For either scalars or lists, data may be typed as in other languages. However, no type declaration is needed.
  • You don't need int $c=0;, instead simply $c=0; (or better my $c=0;).
  • You can do numerical and string processing, as in other languages. Simply use a variable in the right context:
    for (my $i=0; $i < $j; $i++) ... presumes $i is an integer
    my $z = sqrt(2); will put a floating point number in $z
    print $m . " you handsome devil\n" will output $m as a string
  • When you use a list item in scalar context, Perl tries to do the right thing.
    my @arr=("x", "y", "z"); print @arr;
  • If you really need to get a number to output in a particular format, try something such printf. For example:
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(time);
    $year = sprintf("%02d", $year % 100);

First ... Back ... Next ... Last ... (Notes home)

UAF Computer Science
Prof. Greg Newby