Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hello fellow monks,

I am hitting an embarrassingly basic stumbling block, getting confused between doing things the O-O way, and the "Perl" way. (Which, to my mind, can sometimes be two overlapping and sometimes conflicting goals!) In the end, I want something that is elegant, functional, and reusable.

I think what I'm really looking for here is a general Perl design pattern. For the purposes of this discussion, though, consider a "User" object in the system that has attributes "username", "real_name", and "email_address". There are also methods set_password, and check_password. (Implementation detail: Passwords are stored one-way encrypted, so these methods are necessary to do the crypt steps). As you might imagine, Users are stored in an SQL database. The above, I think is fine so far, yes?

Now, here is where I'm stumbling:

Who creates users? If I call User->new(), should that actually create a database entry?

Where does the search() function go? (For example, if I want to find all users with a particular email address). That doesn't seem like it should be a method of User to me. Should I have some kind of aggregation class like a UserSearch class?

When do I commit the User back to the database? Do I do this automatically whenever an attribute is updated, or through an explicit $user->write()? Does SQL code even belong in the User class? Or should the User instance be somehow passed to another specialized class that will do the actual storage functions?

Finally, can you point me to any good existing implementations of something similar?

Again, the User class is just an example. That being said, I want as much code re-use as possible, so if--for example--some of the DB and search mechanisms are better off put into a more generic class or library, suggestions there would be useful, as well.

A lot of questions, to be sure! Thanks for reading.


In reply to O-O design, aggregation of objects by wanna_code_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (7)
    As of 2014-09-19 00:19 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (128 votes), past polls