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. 🙂


Leave a Reply

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

You are commenting using your 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