#!/usr/bin/perl


################PRELIMINARY

#test.pl – should be called hello_world.pl
#your first perl program
print "\n\n";
print " hello everyone\n";


# beforehand, do 'perl -v'
# beforehand, do 'which perl'
# to run test.pl, do;
# chmod +x test.pl'
# then, './test.pl'

# there are not much you can do at this point.
# so learn something more...



###############BASICS

##### variables and data type
#they all start with funny characters...

# scalar (as opposed to list)
# you can use variables without first declearing.
# perl variables are context dependent

$name = "shlomo";   #perl sees this is a string
$n = 123;      		#perl sees this as a number
print " scalar variables \n";
print " $name \n";
$name = 123; 		# "shlomo " is gone.
print " $name \n";

#array (a kind of list. the other kind is hash)
#notice when you want a element of array, you use $ instead of @

@class3157 = ("shlomo","weijen","edward");
@numbers = (1,2,3,4,5);
# get length
$a = @class3157;
#array reference
$ref = \@class3157;
#deref
$derefed = $$ref[0];
#anonymous array
($first,$second)= @number;

print " \n\n";
print " this is array if you just feed it to the print. not pretty :";
#print @class3157 . "\n"; #this gives the length. scalor context
print @class3157;
print "\n";
print " this is array length :";
print $a . "\n";
print " this is array 0th element :";
print $class3157[0] . "\n"; 
print " this is array -1th element :";
print $class3157[-1]. "\n";  
print " this is array 14th element -- which is not initialized. noting.:";
print $class3157[14]. "\n";  
print " this is reference to the array. :";
print $ref. "\n";  
print " this is dereference to the array. :";
print $derefed. "\n";  

# hash -- associtive array -- the other kind of list
%phonelist = {adam=>718, barry=>345};
%phonelist = {"adam",718,"barry",345};
$adam_phone = $phonelist{"adam"};  #is 718
print " \n\n";
print " adam's phone number from the hash. :";
print $adam_phone. "\n";  

#test2.pl -- snipet
#What does this do?
#	$name = getnextemail($sometext);
#	if ($emailcount{$name})  {
#		$emailcount{$name}++;
#	}else{
#		$emailcount{$name} = 1;
#	}

####################statement and other things
#but, we should cover operator and logic structure first.
#operators are same as java/c/c++
#if, else, elif example here... not really necessary

#example of while loops
print " \n\n";
print " while ... \n";
$length = @numbers;
$i= 0;
while ( $i < $length ) {
	print "i=",$i," numbers[i]=", $numbers[$i],"\n";
	$i++;
}
#example of for loops
print " for ... \n";
$length = @numbers;
$i= 0;
for ( $i=0; $i<$length; $i++ ) {
	print "i=",$i," numbers[i]=", $numbers[$i],"\n";
}
# foreach
print " foreach ... \n";
foreach $e (@numbers) {
		print "e=",$e,"\n";
}

# nifty syntax in conditional statement/loop construct
$tmp = 0;
print "funny way of saying samethings. if ...\n" if ($tmp < 10);
print "funny way of saying samethings. unless ...\n" unless ($tmp > 10);
#this is gramatical, but don't do this. its infinite loop.
#print "hello world!\n" while ($tmp < 10);
#print "hello world!\n" until ($tmp > 10);

#foreach with hash -- often used examples...
#test3.pl -- snipet
#What does this do?
#	$name = getnextemail($sometext);
#	if ($emailcount{$name})  {
#		$emailcount{$name}++;
#	}else{
#		$emailcount{$name} = 1;
#	}
#foreach $k (keys %emailcount) {
#	print "$k = "; 
#	print $emailcount{$k};
#	print "\n";
#}


#example of next and last would be here... if have time

#reserved variables... using foreach for the heck of it
#print out those variables...
print "\n\n";
print " reserved variables... \n";
@b = (2,4,6,8);
foreach (@b) {
	print " current \$_ is: ", $_,"\n";
}
#more reserved variables...
#$/ = input record separator (default is newline)
#$$ = process id of the perl process running the script
#$< = real user id of the process running the script
#$0 = (0=zero) name of the perl script
#@ARGV = list of command-line arguments
#%ENV = hash containing current environment
#STDIN = standard input
#STDOUT = standard output
#STDERR = standard error
#showing some of it....
print " current \$\$ is: ", $$,"\n";
print " current \$\/ is: ", $/,"\n";
print " current \$0 is: ", $0,"\n";



###############MORE BASICS

#SUBROUTINE
#this is snip of sub1.pl
print "\n\n";
print " example of subroutine... ";
testsub();
sub testsub{
	print " hi everyone\n";
}

#passing by value
#this is a snip of sub2.pl
$n = 45;
print "\n\n";
print " example of passing by value \n";
print " n is now $n\n";
testsub2($n);
print " n is now $n\n";

sub testsub2{
	$a = shift;
	#alternatives. doing the same thing
	#$a = @_[0];
	#($a) = @_;
	#$a = shift;
	print " in testsub2 = $a\n";
	$a++;
	print " in testsub2 = $a\n";
}


#passing by reference...
#this is sub3.pl
$n = 45;
print " example of passing by reference \n";
print " n is now $n\n";
testsub3(\$n);
print " n is now $n\n";

sub testsub3{
    $a = shift;
    print " in testsub3 $a\n";
    print " in testsub3 $$a\n";
    $$a++;
    print " in testsub3 $$a\n";
}


#different subs
#this is sub4.pl snip
($first,$last) = getname();
#same thing, different way of saying it
#($first,$last) = &getname();
print " First is $first \n";
print " Last is $last \n";
print " changing the context... \n";
#i couldn't get this to work...
#$fullname = getname();
$fullname = getname_full();
print " First is $first \n";
#return the fill name as a string
sub getname_full(){
	return "shlomo hershkop";
}
#return name split
sub getname(){
    return ("shlomo","hershkop");
}
