#!/usr/bin/perl -w
########################
#
# Perl data types
#
# Summary of material from chapter 4 in Johnson's book.
#
# - Jim Mahoney, Sep 15 2003
#
########################
use strict;

# --- Scalars ----------

my $integer    = 12;
my $float      = 14.233;
my $scientific = 2.3e-9;

my $hex        = 0xAAFF;    # base 16 (leading 0x)
print " hex 0xAAFF is '$hex' in base 10\n";

my $octal      = 0177;      # base 8  (leading 0)
print " octal 0177 is '$octal' in base 10\n";

print " A backslash \\   \n";
print " A quote      \"  \n";

my $funny_case = "sOme FuNNY caSE woRDS";
print " original: '$funny_case' ; lower: \L'$funny_case' \n";

my $stringA    = "This is a string with a number : $float. ";
my $stringB    = 'So is this.';
my $stringC    = q{And this};
my $stringD    = qq{And this with integer $integer};
my $here_is    = <<"END_INSERT";
 And even this.
 It's called a "here is" document, and
 is just another kind of string with a really weird quote. 
END_INSERT

# -- operators ----------------

my $x = 2;

$x = $x + 4;
$x += 4;         # same

$x = $x + 1;
$x++;            # same

my $copies = ".-" x 20;    # multiple copies of string or list
print $copies, "\n";

my $concatenation = "this and " . "that";
my $toString      = "" . (1/10) . "0000";

my $toNumber      = 0 + "12.5000";

my $modArithmetic  = 20 % 3;    # remainder after dividing 20 by 3.


# -- arrays ----------------

my @list       = ( 1, 10, 20, 50 );
my @bigger     = ( @list, "hi", @list );        # lists are always flattened.

print "A list : \@bigger: ( @bigger )\n";
print "Same outside quotes : " , @bigger, "\n";
print "Same using join(',',\@array) : (", join(',', @bigger), "\n";

print "First item in \@list is ", $list[0], "\n";    # Note: use of $ here.

my $string     = "zero,one,two,three,four,five";
my @words      = split(',', $string);         # opposite of join()
print "string is '$string'\n";
print "word with index 2 is '$words[2]'\n";   # Note: $words[2], not @words[2]

my @slice      = @list[3,0];     # not $list[3,0] : more than one element

push    @words, "right";
unshift @words, "left";

print "after push, unshift : @words \n";

my $fromLeft = shift @words;      # shift element off left
print " shifted off '$fromLeft'; remaining is '@words' \n";

my $fromRight = pop @words;       # pop element off left
print " shifted off '$fromRight'; remaining is '@words' \n";

# many other list operators : sort(), map(), grep(), ...


# --- hashes ---------------------

# Extremely powerful data structures.

my %first_hash;

$first_hash{color} = 'red';     # note use of $ here : one element.
$first_hash{size}  = 6;

foreach my $key (keys %first_hash){
  print " first_hash{$key} = $first_hash{$key} \n";
}

my %second_hash = (
                   name      =>  'Jim Mahoney',
                   age       =>   44,
                   obsession =>   'tango',
                  );

# The => thing means
#   * quote the stuff to my left
#   * put a comma before the next stuff

# --- context :  THIS IS TRICKY --------

# In perl, the evaluation can depend on whether 
# the parser is expecting an array or scalar next.
# In spoken language we do that all the time: 
# If you say "I'm going to the ", then you expect a noun next.
# Well, perl does that too.

my @foo      = (0,1,2,3);            # array context on the right hand side.
my @foomore  = ( @foo, 4, 5, 6);

my $single   = (2);                  # scalar context on the right hand side.
my @list     = (2);

$single   = 2;
@list     = 2;

@list   = $single;           # So what do you think this does
$single = @foo;              # Or this??  (Warning: not what you think...)

                             # Try it to find out...

# --- references -------------

my $number       = 7;
my $ptrToNumber  = \$number;

print " The number is '$number'.  Its address is '$ptrToNumber'. \n";

# de-reference
my $back2Number  = $$ptrToNumber;
my $another      = ${$ptrToNumber};     # or this
my $weird        = ${number};           # or even this ...

# This is only a preview;
# we'll do lots more of with references later this term.

