On Sun, Jan 4, 2009 at 7:42 PM, John W. Krahn <jwkr...@shaw.ca> wrote:
>>> You should include the $! variable in the die string so that you know why
>>> the
>>> open failed. I suggest
>>>
>>>  my @llist;
>>>  {
>>>    open my $fh, '<', $lfile or die "Unable to open '$lfile': $!";
>>>    @llist = <$fh>;
>>>  }
>>
>> Am I right in thinking that the braces here perform two functions:
>>
>> 1) Limit the scope of $fh.
>
> Yes, as soon as the scope ends the file is automatically closed.
>
>
>> If you want to automatically close filehandles as they go out of
>> scope, it looks like IO::File can do that (see
>> http://perldoc.perl.org/functions/open.html at the end of the page).
>
> As of Perl version 5.8 (or was that 5.6?) you don't have to use a module to
> get that functionality.
>
>
>> 2) Improve readability - the purpose of these lines is to get values
>> into the array @llist, let's make it look that way.
>
> Another way to do that:
>
> my @llist = do {
>    open my $fh, '<', $lfile or die "Unable to open '$lfile': $!";
>    <$fh>;
>    };
>

Ah, because do returns the value of the last expression it evaluated.
(http://perldoc.perl.org/functions/do.html)
Thanks, that's pretty sweet.  :)

John

-- 
sub japh{$_=$_[0];s/[$:]/$"/gs;chop;print $&
while/(.)/g;}print chr q:44:,$/ unless &japh
(qq=Just$/another$/Perl$/hacker$/=) && print
"Gosh, that JAPH you wrote just crashed!\n";
John Refior

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to