[ale] OOP in perl
Jim Lynch
jwl at sgi.com
Fri Apr 25 09:53:32 EDT 2003
Oh and it is common to have the last line of a perl module as:
1;
Which does the same thing your return does. That's described somewhere
in the perl module documentation.
Also, you aren't going to get quite what you expect. This block
my %addr = { 'Name' => "",
'Line1' => "",
'Line2' => "",
'City' => "",
'State' => "",
'Zip' => ""
};
Should probably look like:
my %addr = ( 'Name' => "",
'Line1' => "",
'Line2' => "",
'City' => "",
'State' => "",
'Zip' => ""
);
Else %addr will be an empty hash, which I'm sure is not what you were
expecting.
Jim.
Jim Lynch wrote:
>
> Hi, Chris,
>
> Try this:
>
> > bless \%addr, 'Address';
> > return \%addr;
> > }
>
> Christopher Fowler wrote:
> >
> > I'm going to solve my complex data structure issue with OOP. Plus it
> > will help out in the future. I'm starting this but have ran into a
> > sumple issue of not being able to call a method:
> >
> > I have the Perl Bookshelf and one example to do OOP is to use
> > ObjectTemplate. However Object Template is not in my distribution so
> > I'm making an assumption that I can do OOP without using the template.
> >
> > --- Package --
> > package Address;
> > use strict;
> >
> > sub new {
> > my %addr = { 'Name' => "",
> > 'Line1' => "",
> > 'Line2' => "",
> > 'City' => "",
> > 'State' => "",
> > 'Zip' => ""
> > };
> >
> > bless \%addr, 'Address';
> > return %addr;
> > }
> >
> > sub test {
> > print "In test()\n";
> > }
> > # If I do not place a return 1 at the end, I get the
> > # following error
> > #
> > #Address.pm did not return a true value at ./test line 4.
> > #BEGIN failed--compilation aborted at ./test line 3.
> >
> > return 1;
> >
> > -- test.pl ---
> >
> > #!/usr/bin/perl
> >
> > use Address;
> > use strict;
> >
> > my $addr = Address::new();
> > $addr->test();
> >
> > --- Execution ---
> > [cfowler at cfowler OOP]$ perl test
> > Can't call method "test" without a package or object reference at test
> > line 7.
> > [cfowler at cfowler OOP]$
> >
> > ~
> >
> > _______________________________________________
> > Ale mailing list
> > Ale at ale.org
> > http://www.ale.org/mailman/listinfo/ale
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale
More information about the Ale
mailing list