Repost with typos corrected--Instance variable inheritance: is it just me or am I doing something dumb?]

2001-01-29 Thread Christopher L. Everett

Sorry about that.  It was 3 AM after 12 hours beating my brains out.
I guess I didn't have any left.

Check these modules:

package simian;
use fields qw (name);

use strict; #very important!
use Apache;
use Apache::Request;
use Apache::Constants qw(:common);

sub new {
  my $type = shift;
  my simian $self = fields::new(ref $type || $type);
  $self->{name} = 'Jane';
  return $self; 
}

sub name {
  my $self = shift;

  $self->{name} = shift if @_;
  return $self->{name}; # error here, I mean
}

sub handler ($$) {
  my ($self, $q);

  my $r = Apache::Request->new($q);

  # send headers here

  print $self->name;
  return OK;
}

package macaque;
use base "simian";

sub new {
  my macaque $self = fields::new("macaque");
  return $self;
}

return 1;


then put this line in your startup.pl or a  section:

my $tree_climber = macaque->new;

Then set up the mod perl server to use this class for a handler,


  # all the other stuff
  PerlHandler macaque


and try to access it on the web browser.  You will get an error like 
the following:

<<>>

This happens using "use fields" and "use base" or using @ISA
inheritance. 
What the heck am I doing wrong?

  --Christopher



Re: Instance variable inheritance: is it just me or am I doing something dumb?

2001-01-29 Thread Joe Schaefer

"Christopher L. Everett" <[EMAIL PROTECTED]> writes:

> Check these modules:
> 
> package simian;
> use fields qw (name);
> 
> use strict; #very important!
> use Apache;
> use Apache::Request;
> use Apache::Constants qw(:common);
> 
> sub new {
>   my $type = shift;
>   my class1 $self = fields::new(ref $type || $type);
   ^^
ITYM   simian

>   $self->{name} = 'Jane';
>   return $self->{name}; # error here
^

"new" should return a blessed reference ($self here); 
instead you are returning "Jane".  Any later attempts 
to dereference the result of "new" ( aka "Jane") will 
produce an error under strictures.

> You will get an error like the following:
> 
> Can't use string ("Exchange::MyAccount3") as a HASH ref while "strict
> refs" in use at /usr/local/lib/perl5/site_perl/MyApp/Framework3.pm line
> 245.
> 
> What the heck am I doing wrong?

Check line 245 in Framework3.pm ; if it tries to dereference a 
simian object constructed from your simian::new sub, then you'll 
get an error.

More free advice- "fields" are fairly unreliable in 5.005_03, 
especially regarding inherited object attributes.  (I don't know 
if they're more reliable in 5.6, since I don't view 5.6 as reliable 
enough itself :)

I'd recommend you take another approach, like Class::Contract 
or Tie::SecureHash instead.  See Conway's _Object Oriented Perl_ 
for details.


HTH
-- 
Joe Schaefer



Re: Instance variable inheritance: is it just me or am I doing something dumb?

2001-01-29 Thread darren chamberlain

Christopher L. Everett ([EMAIL PROTECTED]) said something to this effect on 
01/28/2001:
> package simian;
> use fields qw (name);
> sub new {
>   my $type = shift;
>   my class1 $self = fields::new(ref $type || $type);
>   $self->{name} = 'Jane';
>   return $self->{name}; # error here
> }

Why aren't you returning $self?

Change the return line to return $self.

(darren)

-- 
The great artist and thinker are the simplifiers.



Instance variable inheritance: is it just me or am I doing something dumb?

2001-01-28 Thread Christopher L. Everett

Check these modules:

package simian;
use fields qw (name);

use strict; #very important!
use Apache;
use Apache::Request;
use Apache::Constants qw(:common);

sub new {
  my $type = shift;
  my class1 $self = fields::new(ref $type || $type);
  $self->{name} = 'Jane';
  return $self->{name}; # error here
}

sub name {
  my $self = shift;

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

sub handler ($$) {
  my ($self, $q);

  my $r = Apache::Request->new($q);

  # send headers here

  print $self->name;
  return OK;
}

package macaque;
use base "simian";

sub new {
  my macaque $self = fields::new("macaque");
  return $self;
}

return 1;


then put this line in your startup.pl or a  section:

my $tree_climber = macaque->new;


Then set up the mod perl server to use this class for a handler,
and try to access it on the web browser:

You will get an error like the following:

Can't use string ("Exchange::MyAccount3") as a HASH ref while "strict
refs" in use at /usr/local/lib/perl5/site_perl/MyApp/Framework3.pm line
245.

What the heck am I doing wrong?

  --Christopher