RE: OO method query



Dermot <paikkos@xxxxxxxxxxxxxx> asked:
I am trying my hand at creating an package and am a bit
unsure about some of the inner working of what I've done.

I recommend http://books.perl.org/book/171


Q3) In new, can I allow for the object create being done with
more argument like my $page = new sendData(type => 'a5'). How
can you detect named arguments when a object is being created.

I have a new method

sub new {
my $class = shift;

# make arguments to new() into a hash
my %args = @_;

my $self = {};

# copy relevant named arguments from hash to $self
# while setting sensible default values.
$self->{'type'} = $args{'type'} || 'a4';

bless($self, $class);
return $self;
}

# creating new object:
my $page = sendData->new( 'type' => 'a5' );

Q2) In get_file_list is the first ARG really $self? I thought
it would be the class? However it seems to know about itself.

That's Perl OO for you. The first argument passed to a method of
a class is always the object via which the call was made.

HTH,
Thomas
.



Relevant Pages

  • Re: ISO recommendations for hash table lib
    ... Can someone recommend a decent hash table library? ... to have multiple hash tables simultaneously, ... one may need linked list techniques to implement hashtables if chaining is ... surely not be the last nor will there be only one hashtable design to ...
    (comp.lang.c)
  • Re: Count Words
    ... Foodbank wrote: ... but am quite unsure of where to move from here. ... a hash table using the above. ...
    (comp.lang.c)
  • Re: what p2p technology to use?
    ... weather stations data. ... If you want a pure peer-to-peer solution, I recommend using a Distributed ... Hash Table. ...
    (comp.os.linux.development.apps)
  • Re: Windows 7 RC
    ... | candidate for Windows 7), can anyone recommend a free anti-virus ... | Yes the hash is identical. ...
    (alt.comp.anti-virus)
  • RFC: field::aliases
    ... This package works with 'fields' to make object instance variables even ... while still validating them at compile time. ... unsure about the name, ... hash. ...
    (comp.lang.perl.modules)