Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

construct a standard object oriented program

by benlaw (Scribe)
on Mar 30, 2002 at 07:36 UTC ( #155409=perlquestion: print w/replies, xml ) Need Help??
benlaw has asked for the wisdom of the Perl Monks concerning the following question:

hi , monks!
i 'd like to build up a class file, i try to search the related topic from web , camel book and black book~
in conclude , i found it's many style for building a Class

is it any standard format for building a class?

is "sub new{ " is 'must' declare for building a class?

  • Comment on construct a standard object oriented program

Replies are listed 'Best First'.
Re: construct a standard object oriented program
by cjf (Parson) on Mar 30, 2002 at 07:53 UTC
Re: construct a standard object oriented program
by rdfield (Priest) on Mar 30, 2002 at 07:45 UTC
Re: construct a standard object oriented program
by chhe (Sexton) on Mar 30, 2002 at 10:33 UTC

    Basically the perl features specific to object-orientation are not many. The rest is a application of a collection of conventions, style and idioms, which is why from my point of view the perl approach is so exciting.

    To define a class in perl from which objects can be created and against which functions can be called, you basically need a perl module with a constructor function, which can be named as you like - conventionally "new" -, you need to "bless" a datastructure of the module and return the "blessed" datastructure from the constructor. The returned "blessed" datastructure, can now be treated like a "object", in such that functions can be called upon it. The "methods" or functions of a blessed "Class" get the instance data of the object always a first parameter.

    The rest is as said very a matter of style and idioms, also inheritance, which is not specifically supported by perl, but with a more general approach, through the @ISA mechanism, where when a function is not found the "objects" package it looks up the @ISA variable for other packages to lookup.

    The fact that much of perls object-orientation is very much a matter of style and idioms is a powerful feature, because you are flexible in terms of design decisions: You make decisions very much depending in the context of your application needs. There are idioms for: different ways of representing an object, addressing privacy, automatically generating accessors methods, object destruction, functions being both class and instance methods, efficient ways of storing attribut data, debugging, loading objects at runtime, runtime dispatch of function calls etc.

    Also there are modules, which support some of these idioms ready to use, like ObjectTemplate and others.

    All this has been decribed, as my colleque commentors have already observed, in other places much better.

      To define a class in perl from which objects can be created and against which functions can be called, you basically need a perl module with a constructor function,

      No, you need a namespace -- defined with package name;

      I've defined a class (albeit a very, very small one) inside of a larger file once.

      Lur: "But if this cape shrinks, consider your species extinct!"

        Correct, that wasn't precise: you can have as many classes you want in a file - the reason for bless taking an optional additional parameter...., well i said basically....

        Chris
Re: construct a standard object oriented program
by darksym (Beadle) on Mar 30, 2002 at 07:50 UTC
    Using sub new as a constructor isn't mandatory although it may make sense for purposes of uniformity. sub new {} could just as easily be sub creatething {} as described in the perl manual pages: perlobj, perlmod, perltoot. Recommended reading. If you want even more info, pickup a copy of the book "Programming Perl" (the irreverent Camel Book). This is a good starting point. Also, take a look at other's modules. Learning by example.
Re: construct a standard object oriented program
by Anonymous Monk on Mar 30, 2002 at 12:53 UTC
    ... and since you want to build a class (or module, in Perl-ish), you definitely must start with h2xs. This will create automatically, the skeleton of your module and is a really time saver.

    Regards,

      h2xs , oh what is it??
      mm, i haven't hear before !

      i try to execute the command... wor, it seems quite difficult for me ~ hehe , is there any example for h2xs? thx!

        "h2xs builds a Perl extension from C header files"

        That's what 'man h2xs' says - you should read it for yourself, but in the meantime, try this command:

        h2xs -A -X FooBar
        And look inside the newly created directory (FooBar) for a pleasant surprise. ;)

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: construct a standard object oriented program
by Anonymous Monk on Mar 30, 2002 at 22:41 UTC
    It really doesn't matter what you call it. I tend to give it a name in context with the name of the module. For instance, I am writing a wrapper around DBI.pm to handle all my database calls and I call the instantiating subroutine connect.

    What is important is that you have a sub that blesses a hash. Objects are really hash references and blessing allows them to call subs from their module as well as the information stored within them.

    To be specific, you need:

    #...
    my $self = bless {};
    #...
    return $self;
    #

    Note that 'return $self' is not required if bless is on the last line of your code and you don't assign the bless to a scalar.

    Hope that helps!
      What is important is that you have a sub that blesses a hash. Objects are really hash references ...
      Objects need not be hash references. Rather, any reference whose referent has been bless()ed into a particular class (read: package). The reference may be an array reference, scalar reference or even a reference to a subroutine.

      /prakash

Re: construct a standard object oriented program
by Anonymous Monk on Mar 31, 2002 at 11:56 UTC
    I wrote a simple OO tutorial a while ago, perhaps it can help you. look for it on http://japh.nu/index.cgi?base=tuts hth, -kane
      oh sorry i can't go there~

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://155409]
Approved by root
help
Chatterbox?
[Eily]: marto you just need a longer cable :P
[Corion]: marto: Yeah, but I'm somewhat wary of installing random USB drivers downloaded from mega.nz , Google Drive or whatever, so Linux wins there due to there being no conflicts and me just having to edit one text file in the worst case, to add the USB vendor
[hippo]: Long USB cable FTW.
[Corion]: Eily: I've thought of that, but I don't like running long cables through the appartment because sooner or later I'll trip over it, pulling at least one device off its stand :)
[marto]: hmm, may have to patch CPAN::Meta to move from search.cpan to metacpan in the META.json/yml files
[Corion]: marto: Heh - it seems that they plan to keep the search.cpan.org links alive for a long time. But still, I plan on moving PM to use/generate the new links
[Corion]: And I think it's better to generate links to the new world instead of keeping the older links alive by generating new versions of them ;)
[marto]: yeah, I guess it's supposed to be a permanent redirect, but better to make the move where possible

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (12)
As of 2018-05-23 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?