On 03/09/2011 21:34, Uri Guttman wrote:
Ron Weidner<xecro...@yahoo.com>  wrote:

first off, arrays have nothing (or little) to do with OOP. you are using
arrays inside an object but the code at that point is just plain perl.

That is a strange assertion. So OO software is more to do with scalars?
Or hashes? I will buy that Perl objects are generally blessed references
to hashes, but this is an anonymous array as the value of the base hash
object.

 In a module I have code that looks like this...
 sub add_widget
 {
      my $self = shift;
      my $new_widget = shift;
      push ( @{$self->{WIDGETS}}, $new_widget );
 }

Ron, you would befriend more Perl authors here by dropping the
unnecessary parentheses:

  push @{$self->{WIDGETS}}, $new_widget;

 sub get_widgets
 {
      my $self = shift;
      return $self->{WIDGETS};
 }

 I'm writing a test that is failing at runtime with the following error.

 Can't call method "attrib" on unblessed reference at ./test.pl line 40.

 $config = new Widget();

That is an "indirect method call" and a nice syntax, especially because
it looks like C and reads like English, except that it is ambiguous.
Suffice to say that it is bad manners to call the constructor that way
(and even those who denounce it will have to look up why it is so bad).

you should call that with a direct method call and declare it with
my. also config is a bad name for a widget object, even in an example.

OK, so Uri says never to call an object $config, so $config2 should
suffice. It's not a Widget object Uri, it's the thing that has an array
of Widgets.

        my $config = Widget->new();

There is a big difference between

  Widget->new();

and

  Widget->new;

and more often than not you will want the latter. Much like

  use MyModule;

is more likely to be right than

  use MyModule ();

 my $i = 0;
 for ($i=0; $i<3; $i++)

don't declare $i before the loop. it can be declared in it. also use
perl style loops and not c style when you can:

        foreach my $i ( 0 .. 3 ) {

Yes about the declaration, but Uri means

  foreach my $i (0 .. 2) {
    :
  }

 {
      my $w = new Widget();
      $w->add_attrib("name", "widget".($i+1)); #add_attribute is tested and 
working

you don't show that method. also your comment's method name doesn't
agree with the actual method name. be consistant.

No he doesn't. But it's nothing to do with the problem Uri. Also, it's
'consistent'.

      $config->add_widget($w); #this is what I'm testing

and where is the code for this?

He hasn't shown it. Ron is a beginner.

 }

 foreach my $w ( $config->get_widgets() )
 {
      #attrib is tested and working, but this foreach loop is not known to be 
valid code yet.
      print $w->attrib("name"); #<----  This is line 40.

again, where is the code for this method? usually you can't debug method
calls without seeing the method code.

uri

So Uri skips over the likely cause of the problem: that the
'get_widgets' method probably returns an array reference rather than a
list.

My preference would be to write something similar to

  my $widgets = $config->get_widgets;
  foreach my $w (@$widgets) {
    print $w->attrib("name");
    print "\n";
  }

Cheers,

Rob




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to