#One piece of data can be used as a reference to some other piece of data. #Sometimes the piece of data is called a pointer (Pascal or C). #normal scalar example print ":: Normal scalar example ::\n"; $a="Stones"; $b=$a; print '$a = ',"$a\n"; print '$b = ',"$b\n"; $a = "Bread"; print 'after changing only $a'; print "\n"; print '$a = ',"$a\n"; print '$b = ',"$b\n"; print "\n"; #reference example, $ref -> $a, $ref contains address print ":: Reference example 1 ::\n"; $a="Stones"; $ref=\$a; #syntax to create pointer to some scalar print '$a = ',"$a\n"; print '$ref = ',"$ref #this is a meaningless memory address\n"; print '$$ref = ',"$$ref\n"; #syntax two $$ to follow reference $a = "Bread"; print 'after changing only $a to bread'; print "\n"; print '$a = ',"$a\n"; print '$$ref = ',"$$ref\n"; print "\n"; #reference example, $ref -> $a, $ref contains address print ":: Reference example 2 ::\n"; $a="Stones"; $ref=\$a; print '$a = ',"$a\n"; print '$$ref = ',"$$ref\n"; $$ref = "Sticks"; print 'after changing only $$ref to sticks'; print "\n"; print '$a = ',"$a\n"; print '$$ref = ',"$$ref\n"; print "\n"; print ":: Reference example 3, to break a reference ::\n"; $ref = "Break"; #assign it to a scalar print '$ref = ',"$ref\n"; print "\n"; print ":: Reference example 4, chain references ::\n"; $book = "Lord of the Rings"; $bref = \$book; $bref2 = \$bref; print '$$$bref2 = ',"$$$bref2\n"; print "\n"; print ":: Reference example 5, reference to Arrays ::\n"; @trees = qw (oak cedar maple apple); #quick word keyword $aref = \@trees; #point to array print $$aref[0],"\n"; print @$aref[0],"\n"; #same effect print "\n"; print ":: List of Lists, something like 2D array (not exactly the same) ::\n"; @list_of_lists = ( [qw (Mustang Bronco Ranger)], [qw (Cavalier Suburban Buick)], [qw (LeBaron Ram)], ); print "row 0, col 1 = ",$list_of_lists[0][1],"\n"; print "Last element (no of rows) = ",$#list_of_lists,"\n"; print "Last element (no of columns) of row 1 = ",$#{$list_of_lists[1]},"\n"; #there are other better ways to declaring, like using push