Parenthesis
⇐ Back to Data: Lists, Scalars, and Hashes oh my!
Questions, comments, or corrections? Send me a message.
The novice perl programmer may be somewhat confused as to what all those different types of braces mean, since there are 3 kinds of braces and they all do very different things.
The most common are the parenthesis, or (). This is the generic list operator, everything in them is a list of some sort. Most commonly this is used with subroutines and to populate lists, but is also useful for breaking apart lists in to scalars. For example, the stat() function returns a list. While using the list isn't cumbersome, it can be confusing having to remember $st[9] is the mtime of the stat call. To make it easier you could break it apart like this: (ripped from the perlfunc man page)
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);Now $dev, $ino, etc, have the parts of the stat in them available by scalar name instead of some array by index. Now you could just use $size to report the size instead of $st[7]. Also useful when writing your own functions that return a list of data as you can break up the list of any function call. Note that it is also valid to pull apart an array like this since an array is really just a special type of list. So if you did this:
@st = stat( $filename ); .. ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = @st;You get both an array (@st) and the items broken out in scalars by using the list of @st. The opposite is also true, you could populate an array from a list:
@my_list = ( 'red', 'green', 'blue' );To initalize the @my_list to contain the elements red, green, and blue.