Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: A Few Questions About IO::File

by hdb (Monsignor)
on Mar 18, 2013 at 14:18 UTC ( #1024061=note: print w/replies, xml ) Need Help??


in reply to A Few Questions About IO::File

According to module documentation on IO::File on CPAN undef $fh closes the file automatically. In contrast to Python, in Perl there is more than one way to do it. So both of your options are valid.

getlines comes from IO::Handle and is safe in list context according to its documentation.

For every complex problem there is an answer that is clear, simple, and wrong. H. L. Mencken

Replies are listed 'Best First'.
Re^2: A Few Questions About IO::File
by rpetrelli (Novice) on Mar 18, 2013 at 14:58 UTC

    Thanks for the prompt answer. So both methods are valid and safe? Because like I mentioned previously, the $fh->close method fits better in my brain.

    I read the IO::File documentation. While I understand that getlines is supposed to be used in list context, using it directly by chaining the methods is not mentioned in there and I'm wondering the safety of using such method.

      "I read the IO::File documentation. While I understand that getlines is supposed to be used in list context, using it directly by chaining the methods is not mentioned in there and I'm wondering the safety of using such method."

      File handles are scalars, so you can push them onto arrays like this:

      push @all_handles, $fh;

      And pop them off like this:

      my $fh = pop @all_handles;

      And you can loop through an array of filehandles like this:

      for my $fh (@all_handles) { ...; # do something with $fh }

      And you can get a count of how many filehandles are in your array of filehandles like this:

      my $count_filehandles = @all_handles;

      There is no need to mention any of the above in the IO::File documentation because this is simply how arrays work in Perl. The fact that they're arrays of filehandles is not significant - they could be arrays of integers, or XML documents, or whatever.

      Similarly, there's no need to mention in the IO::File documentation that methods which return an object can be chained. That's just how method calls work in Perl (indeed, in most programming languages that support OO).

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
        Similarly, there's no need to mention in the IO::File documentation that methods which return an object can be chained. That's just how method calls work in Perl (indeed, in most programming languages that support OO).

        Thanks for the answer. Since I'm still relatively new to perl, I'd like a clarification first on what is the preferred way to do something in perl from the community (especially since OOP in perl is so raw and different from what I've encountered in other languages, I tried not to hold any assumption)

      When you chain the commands you will not get a handle to close the file. I have to admit that I am not so sure myself when the file will get closed...

        It will close when Perl hits the semi-colon, because that is when Perl realises the object has gone out of scope.

        Quick demo:

        use v5.14; package Foo { sub new { my $class = shift; say "new"; bless [] => $class; } sub some_method { my $self = shift; say "some_method"; return $self; } sub other_method { my $self = shift; say "other_method"; return 123; } sub DESTROY { my $self = shift; say "DESTROY"; } } say "START"; say Foo->new->some_method->other_method and say 456; say "END";

        DESTROY happens after "456" is printed, but before "END".

        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1024061]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2018-11-15 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (190 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!