in reply to Re: Abstract Factory
in thread Abstract Factory

I've already seen it http://www.perl.com/pub/a/1999/09/refererents.html

Replies are listed 'Best First'.
Re^3: Abstract Factory
by skx (Parson) on Oct 04, 2005 at 10:53 UTC

    What part don't you understand?

    Steve
    --
      package Greet::Repeat; sub new { my $class = shift; my $self = { greeting => shift, repeat => shift, }; return bless $self, $class; } sub greet { my $self = shift; print ($self->{greeting} x $self->{repeat}); } 1;
      doesn't understand the role of shift and bless, completely; where we have greeting and repeat are being shift.
        I had same problem, Read : bless and shift
        package Bug; sub print_me { my ($self) = shift; # The @_ array now stores the arguments passed to &Bug::print_me # The rest of &print_me uses the data referred to by $self # and the explicit arguments (still in @_) } or, better still: package Bug; sub print_me { my ($self, @args) = @_; # The @args array now stores the arguments passed to &Bug::print_m +e # The rest of &print_me uses the data referred to by $self # and the explicit arguments (now in @args) }
        $nextbug = { id => "00001", type => "fatal", descr => "application does not compile", }; To turn that anonymous hash into an object of class Bug you write: bless $nextbug, "Bug";
      #!/bin/perl package first; use strict; use warnings; sub new{ my $class = shift; my $type = shift; return bless \$type, $class; } sub greet { my $type = shift; print "\n hello got something .. $$type \n"; } 1; package AFactory; use strict; use warnings; sub get_new { my $class = shift; my $type = shift; return $class->new(@_); } 1; my $greeter = AFactory->get_new("first","dow dow"); $greeter->greet(); print "hellow\n";
      !!!error is : Can't locate object method "new" via package "AFactory" at new.pl line 31.

        Can't locate object method "new" via package "AFactory" at new.pl line 31.

        That's because package "AFactory" hasn't got any method called "new". See?

        Did you think it should get it from a parent class, the one called "first" maybe?
        Then you need to specify that using either use base 'first'; or push @ISA, 'first';.

        Cheers, Sören