Some More Perl Facts

  • In Perl, every parameter is passed by reference. If we convert the implicit @_ array to named parameters, that’s when we switch from call by reference to call by value, which is obvious. Literals, if any, that are passed to a function are strictly read-only. Any attempt to modify them will result in an exception.
  • The return value of a function is the value of the last expression evaluated.
  • If an array or hash is passed to a subroutine, then they will be list interpolated. That means that the array contents or hash contents are just expanded and passed to the subroutine. In that sub routine, the array or hash cannot be accessed as a single entity, however the values modified are still reflected.
  • There is a keyword in Perl called wantarray that will be true (in Perl sense) if the sub routine is called in a list context, false otherwise.
  • Just like the way in which the input parameters are list interpolated, the returned parameters are also flat, so assigning them to multiple arrays is a wrong thing to do. Everything will go to the first array.
  • The & thing has more confusion to it than one can expect. When a function is called like &foo;, the parameter list of the caller is passed to the function. However, when it is called like &foo(), the parameter list is empty. The & thing should be placed as a prefix to the function whenever the function name is used in places like defined, undefined etc. Also the presence of & means that type checking will be disabled.

I think that finding and understanding the basic rules of a programming language is an essential thing to do rather than remembering the rest of the gory details. Because the latter follows from the former and the former is, in general, more compact and can easily fit inside one’s head. :-)

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.

Follow

Get every new post delivered to your Inbox.