-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Read On.
On Tuesday 11 February 2003 09:09 am, Soren Harward wrote:
> I humbly appeal to the perl gurus for a "bless"ing. Here is some sample
> code:
>
> sub ref_print_number($) {
> (my $name) = @_;
Since you are using a single variable, use shift instead of @_;
Also, it is a little more standard to have the 'my' declaration
outside of
the parens eg. my ($name), since you are encapsulating the different
variables in list context.
> return sub {
> (my $foo) = @_;
> print "I was told to print number $foo by $name\n";
> };
> }
>
> The following works:
> #input
> 8: $foo->{'alice'} = ref_print_number('alice');
the hashreference foo gets a subroutine reference and assigns it to an element
named alice. ok.
> 9: $foo->{'alice'}(16);
> #output
execute the subroutine located in the hash element of alice, passing in 16 as
a parameter. that's ok.
> I was told to print number 16 by alice
>
> The following does not:
> #input
> 8: $foo->{'bob'} = ref_print_number('bob');
> 9: $foo->bob(16);
You are telling perl now that bob is a method of a foo object, which you have
never defined. The way you have set this pseudo-package up is that you need
to put the hash element marks around the 'bob ({})... just leaving it empty
won't do it.
> #output
> Can't call method "bob" on unblessed reference at ./rt.pl line 9
>
> Nor does:
> #input
> 8: $foo->carol = ref_print_number('carol');
> 9: $foo->carol(16);
Same as before.
> #output
> Can't call method "carol" on unblessed reference at ./rt.pl line 8
>
> So how exactly do I "bless" my methods? Do I need to construct the
> method differently?
perldoc -f bless, or read it on perldoc.com.
Bless is your tool for making objects (basically). What you should also read
is just the whole oo tutorial for perl, perldoc perltoot is the first step,
it should explain everything you need for now.
Basically, your problem is that you aren't creating a real object, you are
just creating a hash reference of code references, which then must be pulled
out of the reference.
Yes perl can use a hash reference as the 'skeleton' for an object, as well as
arrays, scalars, and tied variables (Check out Damians excellent OO Perl book
by manning to see the good and the twisted examples.). And with those, you
make method calls as you are trying to do. But as you have it setup, no. I
guess if you really wanted, you could set up autoload to do what you want,
but it would be 100% better style/structure, as well as easy to just write it
correctly as a package.
>Do I need to convince my boss that he really should
> learn Python so I can write programs like this in it?
Nope.
- --
- --Jayce^
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
iD8DBQE+SSZYA10/0O8cAHgRArzWAKCzlLrw/MouMV5rYfGYF/WBR+RjdgCeKpFV
ayRQ1gzCCBo2eE0mdK5C3lk=
=ibM1
-----END PGP SIGNATURE-----
____________________
BYU Unix Users Group
http://uug.byu.edu/
___________________________________________________________________
List Info: http://phantom.byu.edu/cgi-bin/mailman/listinfo/uug-list