Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by tobyink (Abbot)
on Mar 18, 2013 at 15:19 UTC ( #1024084=note: print w/replies, xml ) Need Help??


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

"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

Replies are listed 'Best First'.
Re^4: A Few Questions About IO::File
by rpetrelli (Novice) on Mar 18, 2013 at 15:26 UTC
    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 think I read somewhere that Larry wall borrowed the object system directly from python. It's evident by the way both languanges need to unpack the invocant $self manually.

        While they are similar in concept, in perl I have to do a lot of manual works and boilerplates just to create a class with a bunch of accessors and mutators methods.

        And I am familiar with Moose, since my original reason to learn perl was to use the Catalyst framework which uses Moose (roles and Moose's type checking ability is da bomb). But I somehow got sidetracked and currently learning the guts of perl's object system. It's all good, fortunately since this is only a side project and I'm not hard pressed on time.

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

        Thanks for the pointer.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1024084]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2018-07-16 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (342 votes). Check out past polls.

    Notices?