Perl facts

Some facts about perl that I learnt today:

  • References are scalars. It is the third scalar, the first two being strings and numbers.
  • Also, references are of two types. Symbolic references just refer to a variable name and not the contents per se. Hard references refer directly to the value. One can compare this to the symbolic and hard references we find in file system.
  • One thing to note in perl is that non-scalar data structures like arrays and hashes are linear. That is they can hold only scalars and not arrays and hashes. So when we refer to array of arrays, array of hashes etc., we merely mean that the array contains references to arrays or hashes, but not arrays or hashes themselves.
  • References can be created for anonymous scalars, or constants like 7, “nagp” also.
  • A reference can be created by merely pre-pending a \ to the variable or to the anonymous version.
  • References to anonymous arrays are created with [ and ], instead of with normal ( and ).
  • References are dereferenced with a ->.
  • References to anonymous hashes are created with { and }. with key => value pairs separated by commas. The value can be another mess of data structures.
  • Perl does no implicit referencing or dereferencing. When a scalar holds a reference, it just behaves like a scalar and does not magically do any auto mapping.
  • If we know what the dereferenced type is, then we can dereference the scalar. For example, if we know $foo is a reference to an array, then that array can be referenced as @$foo. If it is a hash, it can be as %$foo. And if it is just a scalar, then it is $$foo.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s