From: sln on
On Thu, 10 Dec 2009 14:58:48 -0500, Sir Robert Burbridge <rburbrid(a)cisco.com> wrote:

>On 12/10/2009 07:34 AM, Justin C wrote:
[snip]
>>
>> Yup, this is where you're losing me. I'll go to the documentation, I
>> fear it's going to take a little time reading, re-reading, and
>> re-re-reading to get some of this stuff. As I'm getting older I find
>> that practical (physicla) stuff goes in more easily, and abstract stuff
>> is getting harder to understand... that's no fun when the practical
>> stuff is no longer easy to do!
>>
>> Justin.
>>
>
>A popular phrase floating around is that Perl's OO is "bolted on".
>That's actually a good way of looking at it. A perl object is just a
>hash (usually) on to which you bolt some subs.
>
>That's really all =)
>
>Like using a glue gun to attach a pinwheel to your cell phone ... now
>you can do $phone->spin()
>
>-Sir
>
I get what you mean but, I always thought a Perl object
was an instance of a class with data and subs.

C++ an object is as little as an instance of a structure
where functions can be added or not. Of course there are
many more capabilities.

I wonder if Perl uses vtable like logic? Don't know.

-sln
From: Sir Robert Burbridge on
On 12/10/2009 03:13 PM, sln(a)netherlands.com wrote:
> I get what you mean but, I always thought a Perl object
> was an instance of a class with data and subs.
>
> -sln

Nah, it's much looser than that. A perl object is just any ol'
reference that's currently wearing a certain hat. The reference doesn't
get altered in any meaningful way (at least not that I've ever
encountered!).

Weirdly the "any ol' ref" part means you can do things like this too:
my $code_ref = sub { ... };
bless $code_ref, "Some::PKG";
### Execute some method from the "object"
$code_ref->some_method();
### Execute the "object" directly!
$code_ref->();

But anyway, check this out:

#!/usr/bin/perl
use strict;

sub hr { print '-' x 20, "\n"; }

### Create a hash ref.
my $slacker = {action=>'nuthin.'};
print $slacker, "\n";

eval {$slacker->dance()}; print $@;
eval {print $slacker->report()}; print $@;

hr;
bless $slacker, 'Blogger';
print $slacker, "\n";
eval {$slacker->dance()}; print $@;
eval {print $slacker->report()}; print $@;

hr;
bless $slacker, 'Moxie';
print $slacker, "\n";
eval {$slacker->dance()}; print $@;
eval {print $slacker->report()}; print $@;

hr;
bless $slacker, 'Blogger';
print $slacker, "\n";
eval {$slacker->dance()}; print $@;
eval {print $slacker->report()}; print $@;


### Two packages with different subs
package Blogger;
sub report {
return "I'm doin' " . shift->{'action'} . "\n";
}

package Moxie;
sub dance {
shift->{'action'} = 'a jig!';
}

1;

Run it to get this output...

-- output:
HASH(0x84ac880)
Can't call method "dance" on unblessed reference at ./f.pl line 10.
Can't call method "report" on unblessed reference at ./f.pl line 11.
--------------------
Blogger=HASH(0x84ac880)
Can't locate object method "dance" via package "Blogger" at \
./foo.pl line 16.
I'm doin' nuthin.
--------------------
Moxie=HASH(0x84ac880)
Can't locate object method "report" via package "Moxie" at \
./f.pl line 23.
--------------------
Blogger=HASH(0x84ac880)
Can't locate object method "dance" via package "Blogger" at \
./f.pl line 28.
I'm doin' a jig!

Notice that the address of the object is the same before and after all
the blessings (0x84ac880 here). Doing the same thing with Data::Dumper
would show that nothing's changing at *all* in the "object" except the
package being associated with it.

-Sir
From: Ben Morrow on

Quoth Sir Robert Burbridge <rburbrid(a)cisco.com>:
> On 12/10/2009 03:13 PM, sln(a)netherlands.com wrote:
> > I get what you mean but, I always thought a Perl object
> > was an instance of a class with data and subs.
>
> Nah, it's much looser than that. A perl object is just any ol'
> reference that's currently wearing a certain hat. The reference doesn't
> get altered in any meaningful way (at least not that I've ever
> encountered!).

The *ref* itself isn't changed at all. The object (the thing the ref
points to) just has the string you supply stuffed into its 'class' slot.
You can create a completely fresh ref, and it's still an object:

my $o = bless {}, "Foo";
my $o2 = \%$o;
# $o2 is a brand new ref to the same object

> Weirdly the "any ol' ref" part means you can do things like this too:
> my $code_ref = sub { ... };
> bless $code_ref, "Some::PKG";
> ### Execute some method from the "object"
> $code_ref->some_method();
> ### Execute the "object" directly!
> $code_ref->();

Oh, you can do *much* weirder things than that :). Check out the source
for IO::Handle and related modules sometime.

Ben

From: David Harmon on
On Wed, 9 Dec 2009 23:10:17 +0000 in comp.lang.perl.misc, Ben Morrow
<ben(a)morrow.me.uk> wrote,
> In simple terms, an object is just a
>refrence with a bit of magic attached.

"bit of magic attached" isn't simple terms; it's pure obfuscation.
From: Ben Morrow on

Quoth "Newsgroup only please, address is no longer replyable." <bad(a)example.invalid>:
> On Wed, 9 Dec 2009 23:10:17 +0000 in comp.lang.perl.misc, Ben Morrow
> <ben(a)morrow.me.uk> wrote,
> > In simple terms, an object is just a
> >refrence with a bit of magic attached.
>
> "bit of magic attached" isn't simple terms; it's pure obfuscation.

If you say so. Would you rather I said 'an object is a reference to a
SV that is at least a PVMG, with SvSTASH set to point to the stash of
the class it's blessed into'? Perhaps you would like to provide a simple
explanation of Perl objects, suitable for someone who hasn't met the
concept before?

Ben