The proper way to open()

Smylers Smylers at stripey.com
Mon Jan 30 17:21:40 GMT 2012


Roger Burton West writes:

> What's a good way of opening a file as read-only, and failing if it
> doesn't exist?

Like Leo (who mentioned it yesterday) I find it useful to use
Path::Class for all dealings with filenames.

>   open IN,"<$cfg";
>   if (eof IN) {
>     die "bad config file $cfg\n";
>   }

I'd write that as:

  use Path::Class qw<file>;

  my $in = (file $cfg)->openr;

Then use $in instead of IN throughout the rest of the code.

Path::Class is arguably overkill in this case -- where you aren't
actually manipulating the file's path in any way -- but I've found it
preferable to have the simplicity of standardizing on always using
Path::Class everywhere (rather than to open files in different ways
depending on where the filename came from).

Cheers

Smylers
-- 
http://twitter.com/Smylers2


More information about the london.pm mailing list