Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > class global variable?(from Perltoot document)

Reply
Thread Tools

class global variable?(from Perltoot document)

 
 
jh3an
Guest
Posts: n/a
 
      03-11-2008
Please give me your advice
I was reading perltoot document, and I got stuck the following class
global variable concept.
HELP!!

Why global variable should be accessed by hash like this? :
$self->{"_CENSUS"}

Perltoot doc:
Accessing Class Data
It turns out that this is not really a good way to go about handling
class data. A good scalable rule is that you must never reference
class data directly from an object method. Otherwise you aren't
building a scalable, inheritable class. The object must be the
rendezvous point for all operations, especially from an object method.
The globals (class data) would in some sense be in the "wrong" package
in your derived classes. In Perl, methods execute in the context of
the class they were defined in, not that of the object that triggered
them. Therefore, namespace visibility of package globals in methods is
unrelated to inheritance.(WHAT THE HECK HE IS TALKING ABOUT @.@??????)

Got that? Maybe not. Ok, let's say that some other class
"borrowed" (well, inherited) the DESTROY method as it was defined
above. When those objects are destroyed, the original $Census variable
will be altered, not the one in the new class's package namespace.
Perhaps this is what you want, but probably it isn't.
(WHAT THE HECK HE IS TALKING ABOUT @.@??????)
 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      03-11-2008
jh3an <> writes:

> Please give me your advice
> I was reading perltoot document, and I got stuck the following class
> global variable concept.
> HELP!!
>
> Why global variable should be accessed by hash like this? :
> $self->{"_CENSUS"}


Note that that's not just a hash, $self is an object, and
$self->{_CENSUS} in the original text is a reference to a shared lexical
variable, not a package variable.

> Perltoot doc:
> Accessing Class Data
> It turns out that this is not really a good way to go about handling
> class data. A good scalable rule is that you must never reference
> class data directly from an object method. Otherwise you aren't
> building a scalable, inheritable class. The object must be the
> rendezvous point for all operations, especially from an object method.
> The globals (class data) would in some sense be in the "wrong" package
> in your derived classes. In Perl, methods execute in the context of
> the class they were defined in, not that of the object that triggered
> them. Therefore, namespace visibility of package globals in methods is
> unrelated to inheritance.(WHAT THE HECK HE IS TALKING ABOUT @.@??????)


That part relates to the "Class Data" section directly above it. Maybe
you should read that again.

> Got that? Maybe not. Ok, let's say that some other class
> "borrowed" (well, inherited) the DESTROY method as it was defined
> above. When those objects are destroyed, the original $Census variable
> will be altered, not the one in the new class's package namespace.
> Perhaps this is what you want, but probably it isn't.
> (WHAT THE HECK HE IS TALKING ABOUT @.@??????)


<MS Paperclip>It seems you're having trouble grasping the potential
problems with inheritance. Maybe you should just try and build some
inheriting classes and objects and you'll find out</MS Paperclip>



--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-11-2008
jh3an <> wrote:
> Please give me your advice
> I was reading perltoot document, and I got stuck the following class
> global variable concept.
> HELP!!
>
> Why global variable should be accessed by hash like this? :
> $self->{"_CENSUS"}


That isn't a global variable. That is what they recommend using
*instead* of a global variable.

>
> Perltoot doc:
> Accessing Class Data
> It turns out that this is not really a good way to go about handling
> class data. A good scalable rule is that you must never reference
> class data directly from an object method. Otherwise you aren't
> building a scalable, inheritable class. The object must be the
> rendezvous point for all operations, especially from an object method.
> The globals (class data) would in some sense be in the "wrong" package
> in your derived classes. In Perl, methods execute in the context of
> the class they were defined in, not that of the object that triggered
> them. Therefore, namespace visibility of package globals in methods is
> unrelated to inheritance.(WHAT THE HECK HE IS TALKING ABOUT @.@??????)


Is there a particular part you are having trouble with, or is it the
entire paragraph? If the latter, are you familiar with OO outside the
Perl context?


>
> Got that? Maybe not. Ok, let's say that some other class
> "borrowed" (well, inherited) the DESTROY method as it was defined
> above. When those objects are destroyed, the original $Census variable
> will be altered, not the one in the new class's package namespace.
> Perhaps this is what you want, but probably it isn't.
> (WHAT THE HECK HE IS TALKING ABOUT @.@??????)


He is stalking about class inheritance, and the problems with using
global variables therein.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overridden method in perltoot cmic Perl Misc 2 08-13-2009 08:40 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
perltoot bug? himanshu.garg@gmail.com Perl Misc 0 07-11-2006 03:07 AM
A (probable) error in perltoot ( perl5/5.8.0/pod/perltoot.pod, line number 756 ) Himanshu Garg Perl Misc 1 09-21-2003 03:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57