Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

3. Naming Conventions > Reference Variables

Reference Variables

Mark variables that store references with a _ref suffix.

In Perl, you can't give a variable a specific type to ensure that it's able to store only particular kinds of values (integer, string, reference, and so on). That's usually not a problem, because Perl's automatic type conversions paper over most of the cracks very neatly [13]

Except when it comes to references.

It's an all-too-common mistake to put a reference into a scalar, and then subsequently forget to use the all-important dereferencing arrow:

sub pad_str {
    my ($text, $opts) = @_;

    my $gap   = $opts{cols} - length $text;        # Oops! Should be: opts->{cols}
    my $left  = $opts{centred} ? int($gap/2) : 0;  # Should be: opts->{centred}
    my $right = $gap - $left;

    return $SPACE x $left . $text . $SPACE x $right;
}

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint