http://qs321.pair.com?node_id=84005


in reply to Daft text adventure project

Several comments:
References are Scalars
Perl has a few data types, the most common of which are lists, hashes, and scalars. A reference is a scalar. It "refers" to any data type. This is advantagious, because it allows you to put a non-scalar data type where you would normally store a scalar. For example, A hash is a collection of scalars. You can't make $foo{Bar} hold a hash, but you can make it hold a reference to a hash. This means you can have nested data types.

Let's try a simple example for your program. Let's just say that you wanted to have multiple characters. You could have %characters hold them, with their name as the key, so that $characters{'Bob the Fighter'} would hold Bob's info. What is Bob's info? well, he'll have Attributes, Feats, Skills, Level, and so forth. Each of those could be a hash storing more detailed info. Let's initialize a sample:

$characters{'Bob the Fighter'} = { #The brackets create a reference t +o an anonymous hash. 'Attributes' => { # We're nesting a second hash 'Str' => 10, 'Dex' => 10 }, #Done with that hash 'Level' => 1, #Storing a normal scalar 'Feats' => { #Another hash! 'Light_Armor' => 1 #Treating as a boolean } #done with Feats Hash }; #done with hash that is stored in $characters{'Bob the Fighter' +}
To access some of Bob's data, we de-reference the reference with the arrow operator: $character{'Bob the Fighter'}->{'Attributes'}->{'Str'} would equal 10.

References are key to any complicated data manipulation in Perl. I suggest experimenting.