OO in perl

2005-07-19 Thread Beast


Perl Object Oriented Programming is very confusing (to me at least :)
---
package Test;

sub new {}

sub f_one{}

sub f_two{}

1;
---

I can call using

my $test = Test-new-f_one;

But why this not works?

my $test = Test-new-f_one-f_two;


--

--beast


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: OO in perl

2005-07-19 Thread Thomas Bätzler
Beast [EMAIL PROTECTED] asked:

 I can call using
 
 my $test = Test-new-f_one;

Test-new returns an object of type Test; and so your code
is equivalent to

my $obj = Test-new;
my $test = $obj-f_one;

 But why this not works?
 
 my $test = Test-new-f_one-f_two;

This will work if f_one returns a valid Test object.

HTH,
Thomas

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: OO in perl

2005-07-19 Thread Jeff 'japhy' Pinyan

On Jul 19, Beast said:


package Test;

sub new {}

sub f_one{}

sub f_two{}


You've neglected to show us the contents.  I'll assume that new() creates 
and returns an object.  I'll also assume that f_one() and f_two() DO NOT 
return objects.



my $test = Test-new-f_one;


Test-new returns an object, which then has its f_one() method called.


But why this not works?

my $test = Test-new-f_one-f_two;


Because f_one() does not return an object!

--
Jeff japhy Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Attempting OO with Perl - New() method in subclass not working

2005-04-02 Thread Randal L. Schwartz
 Brad == Brad Carlson [EMAIL PROTECTED] writes:

Brad Just to clarify, the only reason I don't need a new() method in the
Brad subclass is because any class-specific logic is contained in the _init()
Brad method for both parent and subclass.  If there were no _init() method,
Brad then both classes would have a new(), right?

Yes.  You can either put your per-class initialization into -new,
and use SUPER::new for parent classes, or have an _init, which your
most-parent-class must remember to call.  The latter is a Smalltalk-ish
standard, where Object class[1] has something like:

new

^self basicNew initialize

and initialize[2] looks like

initialize

^self

Then every derived class that wants additional hooks does:

initialize

super initialize.
my code here.
^self

In Perl terms, this would look like:

sub new {
  my $class = shift;
  my $self = bless [EMAIL PROTECTED], $class;
  return $self-initialize; # and return this
}

sub initialize { # base definition
  my $self = shift;
  return $self;
}

sub initialize { # derived definition
  my $self = shift;
  $self-SUPER::initialize;
  $self-{extra} = thing;
  return $self;
}

By the way, I really really recommend that people who are doing
serious OO get some Smalltalk experience.  Smalltalk got a *lot*
of things right.  Free implementations of Smalltalk for all platforms
are available at www.squeak.org, and there's a wealth of both free
and commercial publications about learning Smalltalk.

[1] Actually, it's in Behavior class.
[2] Defined in ProtoObject, the base class for Object

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
merlyn@stonehenge.com URL:http://www.stonehenge.com/merlyn/
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Attempting OO with Perl - New() method in subclass not working

2005-04-01 Thread Randal L. Schwartz
 Jeff == Jeff 'japhy' Pinyan [EMAIL PROTECTED] writes:

Jeffsub new {
Jeff  my $that = shift;
Jeff  my $class = ref($that) || $that;
Jeff  my $self = $class-SUPER::new();
Jeff  $self-SUPER::_init();

There's a rule of thumb from smalltalk that if you're using a super
call with a method name other than your own, you just broke something.

And you just broke something.

The problem is not here.  The problem is in _init, which also
needs to call SUPER::_init.  And then the rule correctly points
out the solution.  How nice.

Jeff  return $self;
Jeff}

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
merlyn@stonehenge.com URL:http://www.stonehenge.com/merlyn/
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Attempting OO with Perl - New() method in subclass not working

2005-04-01 Thread Jeff 'japhy' Pinyan
On Apr 1, Randal L. Schwartz said:
Jeff == Jeff 'japhy' Pinyan [EMAIL PROTECTED] writes:
Jeffsub new {
Jeff  my $that = shift;
Jeff  my $class = ref($that) || $that;
Jeff  my $self = $class-SUPER::new();
Jeff  $self-SUPER::_init();
There's a rule of thumb from smalltalk that if you're using a super
call with a method name other than your own, you just broke something.
And you just broke something.
The problem is not here.  The problem is in _init, which also
needs to call SUPER::_init.  And then the rule correctly points
out the solution.  How nice.
Oh, right.  April Fool? ;)
--
Jeff japhy Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



Re: Attempting OO with Perl - New() method in subclass not working

2005-04-01 Thread Randal L. Schwartz
 Jeff == Jeff 'japhy' Pinyan [EMAIL PROTECTED] writes:

Jeff Oh, right.  April Fool? ;)

Perhaps that's half right. :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
merlyn@stonehenge.com URL:http://www.stonehenge.com/merlyn/
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Attempting OO with Perl - New() method in subclass not working

2005-04-01 Thread Brad.Carlson
Thank you!

I tried the SUPER-_init() and that took care of it.  However, I donn't
want to force an inherited class to have to know how the parent's new()
works, so I tried this as well (and it worked) - I have the new() call
it's own _init explicitly:

# the new() from Logon.pm   
sub new {
 my $that = shift;
 my $class = ref($that) || $that;
 my $self = $class-SUPER::new();
 $self-Logon::_init();
 return $self;
   }

# the new() from DBLogon.pm   
sub new {
 my $that = shift;
 my $class = ref($that) || $that;
 my $self = $class-SUPER::new();
 $self-DBLogon::_init();
 return $self;
   }

Thanks again, I've been fighting this for quite a while.

-Original Message-
From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED] 
Sent: Thursday, March 31, 2005 7:18 PM
To: [EMAIL PROTECTED]
Cc: beginners@perl.org
Subject: Re: Attempting OO with Perl - New() method in subclass not
working


On Mar 31, [EMAIL PROTECTED] said:

 When I try to assign values to the DBLogon object's attributes, I get 
 the following error: Can't access 'USERID' field in object of class 
 DBLogon at testdblogon.pl line 7.  What am I missing?

The problem is that Logon's new() method is calling LogonDB's _init() 
method.  To get around that, you'll have to do:

   package DBLogon;
   use Logon;
   @ISA = qw (Logon);
   use Carp;

   sub new {
 my $that = shift;
 my $class = ref($that) || $that;
 my $self = $class-SUPER::new();
 $self-SUPER::_init();
 return $self;
   }

-- 
Jeff japhy Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Attempting OO with Perl - New() method in subclass not workin g

2005-04-01 Thread Bob Showalter
[EMAIL PROTECTED] wrote:
 I am trying to build an inheritable object, and I am using examples
 from the Perl Cookbook, 2nd edition, mostly Recipe 13.1, using the
 new and _init methods.

Your subclass does not need (and should not have) a new() method; it can use
the one from the base class.

As Randal pointed out, the subclass _init needs to call
$self-SUPER::_init(), and then do any additional initialization. If there
is no additional initialization in the subclass, you don't need an _init
method there either.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Attempting OO with Perl - New() method in subclass not working

2005-04-01 Thread Brad.Carlson
Okay, why is the subclass's new() not required?  Is it because the
DBLogon's additional field (DBNAME) is added via the _init() method?  So
you end up with something like this:

# Base class Logon new()
sub new {
my $that = shift;
my $class = ref($that) || $that;
my $self = {};
bless $self, $class;
$self-_init();

return $self;
}

# Base class Logon _init()
sub _init {
my $self = shift;
$self-{USERID} = undef;
$self-{SERVER} = undef;
}


Then the subclass DBLogon would no longer have a new() method, but would
retain its _init() method, which would be modified to call the SUPER's
_init as well, right?


# Subclass DBLogon init()
sub _init {
my $self = shift;
$self-SUPER::_init();
$self-{DBNAME} = undef;
}

Just to clarify, the only reason I don't need a new() method in the
subclass is because any class-specific logic is contained in the _init()
method for both parent and subclass.  If there were no _init() method,
then both classes would have a new(), right?

-Original Message-
From: Bob Showalter [mailto:[EMAIL PROTECTED] 
Sent: Friday, April 01, 2005 10:44 AM
To: '[EMAIL PROTECTED]'; beginners@perl.org
Subject: RE: Attempting OO with Perl - New() method in subclass not
working


[EMAIL PROTECTED] wrote:
 I am trying to build an inheritable object, and I am using examples 
 from the Perl Cookbook, 2nd edition, mostly Recipe 13.1, using the new

 and _init methods.

Your subclass does not need (and should not have) a new() method; it can
use the one from the base class.

As Randal pointed out, the subclass _init needs to call
$self-SUPER::_init(), and then do any additional initialization. If
there is no additional initialization in the subclass, you don't need an
_init method there either.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Attempting OO with Perl - New() method in subclass not working

2005-03-31 Thread Brad.Carlson
I am trying to build an inheritable object, and I am using examples from
the Perl Cookbook, 2nd edition, mostly Recipe 13.1, using the new and
_init methods.  

I created a base class Logon and a subclass called DBLogon, in files
Logon.pm and DBLogon.pm, respectively.  All referenced files are copied
below.  I can create a Logon object and print its contents with no
problem (see test program testlogon.pl).  However, my subclass is not
working.  According to Recipe 13.1 in the cookbook, have an _init()
function called by new() can make the class more inheritable (separate
the memory allocation and blessing step from the instance data
initialization step, p. 507-508).  

When I try to assign values to the DBLogon object's attributes, I get
the following error: Can't access 'USERID' field in object of class
DBLogon at testdblogon.pl line 7.  What am I missing?  

#  Logon.pm 
package Logon;
use Carp;

# Constructor
sub new {
my $that = shift;
my $class = ref($that) || $that;
my $self = {};
bless $self, $class;
$self-_init();

return $self;
}

sub _init {
my $self = shift;
$self-{USERID} = undef;
$self-{SERVER} = undef;
}

sub display {
my $self = shift;
my @keys;

if (@_ == 0) {
@keys = sort keys (%$self);
} else {
@keys = @_;
}


foreach $key (@keys) {
$key = uc $key;
my $label = ucfirst lc $key;
print $label: $self-{$key}\n;
}
}

sub userid;
sub server;

sub AUTOLOAD {
my $self = shift;
my $type = ref($self) || croak $self is not an object;
my $name = uc $AUTOLOAD;
$name =~ s/.*://;   # Strip fully-qualified portion

unless ( exists $self-{$name} ) {
croak Can't access '$name' field in object of class
$type;
}

if (@_) {
return $self-{$name} = shift;

} else {
return $self-{$name};
}
}
1;

#  DBLogon.pm 
package DBLogon;
use Logon;
@ISA = qw (Logon);

use Carp;

# Constructor
sub new {
my $that = shift;
my $class = ref($that) || $that;
my $self = $class-SUPER::new();
bless $self, $class;

$self-_init();

return $self;
}

sub _init {
my $self = shift;
$self-{DBNAME} = undef;
}
1;

There are also 2 test programs, testlogon.pm and testdblogon.pm:

#  Logon.pm 
#!/usr/bin/perl

use DBLogon;

my $mylogon = DBLogon-new();

$mylogon-userid(user1);
$mylogon-server(server1);
$mylogon-dbname(db1);

$mylogon-display();

#  Logon.pm 
#!/usr/bin/perl

use DBLogon;

my $mylogon = DBLogon-new();

$mylogon-userid(user1);
$mylogon-server(server1);
$mylogon-dbname(db1);

$mylogon-display();




Re: Attempting OO with Perl - New() method in subclass not working

2005-03-31 Thread Jeff 'japhy' Pinyan
On Mar 31, [EMAIL PROTECTED] said:
When I try to assign values to the DBLogon object's attributes, I get
the following error: Can't access 'USERID' field in object of class
DBLogon at testdblogon.pl line 7.  What am I missing?
The problem is that Logon's new() method is calling LogonDB's _init() 
method.  To get around that, you'll have to do:

  package DBLogon;
  use Logon;
  @ISA = qw (Logon);
  use Carp;
  sub new {
my $that = shift;
my $class = ref($that) || $that;
my $self = $class-SUPER::new();
$self-SUPER::_init();
return $self;
  }
--
Jeff japhy Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response