[ale] OOP in perl
Jim Lynch
jwl at sgi.com
Fri Apr 25 11:25:43 EDT 2003
All of the examples I've seen use hashes, I suspect it is a
requirement. At the risk of p*ssing off perl enthusiasts, IMHO, OOP
perl is a bit of a hack. The language wasn't designed to be OOP hence a
hack. Not to say it won't work and isn't efficient, but it "ain't"
pretty.
You saw the my $ref = { ... } because that's a reference to a hash. You
used
my %addr = which isn't a reference. That's why you have to use the
parens rather than the curly braces to delimit the content.
Jim.
Christopher Fowler wrote:
>
> Does all the data in an object *have* to be stored in a hash?
>
> Could I just not have this:
>
> my $name; my $line1; my $line2; my $city; my $state; my $zip;
>
> This would be like I would do it in Java or C++.
>
> Chris
>
> On Fri, 2003-04-25 at 09:53, Jim Lynch wrote:
> > 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
>
> _______________________________________________
> 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