[ale] OOP in perl

Jim Lynch jwl at sgi.com
Fri Apr 25 09:36:25 EDT 2003


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





More information about the Ale mailing list