Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: A Few Questions About IO::File

by rpetrelli (Novice)
on Mar 18, 2013 at 14:58 UTC ( [id://1024073]=note: print w/replies, xml ) Need Help??


in reply to Re: A Few Questions About IO::File
in thread A Few Questions About IO::File

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.

Replies are listed 'Best First'.
Re^3: A Few Questions About IO::File
by tobyink (Canon) on Mar 18, 2013 at 15:19 UTC

    "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)

        "especially since OOP in perl is so raw and different from what I've encountered in other languages"

        That's an interesting thing to say. It's certainly a lot rawer (is that a word?) than Java or PHP, or many other languages. But Python and Perl's OO systems are actually fairly close to each other:

        • Methods are just functions which take an object as the first parameter.
        • Lack of true private methods; just use the convention of an underscore prefix. (Though Python also has name mangling for double underscores.)
        • Multiple inheritance and a default of depth-first mro.
        • Instances have slots where the class can store arbitrary data, and which do not need to be declared beforehand. (Or at least, this is true in Perl if the instances are blessed hashrefs, as is very common.)

        Moose provides a much more organised way of building classes in Perl if you're fed up of doing it "raw". IMHO, it's more powerful than the OO of pretty much any other mainstream programming language. Though it's just a layer over Perl's built-in crazy way of doing OO, so you can always dip into that as needed.

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

        I wouldn't worry about it too much, read Modern Perl, you'll have the general idea

Re^3: A Few Questions About IO::File
by hdb (Monsignor) on Mar 18, 2013 at 15:19 UTC
    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1024073]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found