Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

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

Singletons and global variables are both manifestations of "global state". Generally speaking, global state is the enemy. (And I don't mean that as an anti-UN conspiracy theorist!)

If the global state is mutable (i.e. different parts of the code can alter it), then it can result in spooky action at a distance. Even if it's immutable, it can result in too tight coupling between different bits of code.

It's certainly best to pass into functions all the data they need to do their job, and pass to object constructors all the data the object needs.

It can be cumbersome to pass around database handles from object to object, but there are patterns that can be followed to help eliminate some of these difficulties. For example, say you have a Document object that creates several Section objects, and needs to pass its database handle on to its sections. You might have something like this...

sub get_header { my $self = shift; Section->new(dbh => $self->dbh, title => "Header"); } sub get_toc { my $self = shift; Section->new(dbh => $self->dbh, title => "Contents"); } sub get_footer { my $self = shift; Section->new(dbh => $self->dbh, title => "Footer"); }

This can be transformed into:

sub make_section { my $self = shift; Section->new(dbh => $self->dbh, @_); } sub get_header { my $self = shift; $self->make_section(title => "Header"); } sub get_toc { my $self = shift; $self->make_section(title => "Contents"); } sub get_footer { my $self = shift; $self->make_section(title => "Footer"); }

This means that the dbh pass-the-parcel happens in just one place instead of many. It also makes it easier for subclasses of Document to override the construction of Section objects.

(My module MooseX::ConstructInstance can help with implementing this pattern.)

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

In reply to Re: Access to single object from multiple other objects by tobyink
in thread Access to single object from multiple other objects by elTriberium

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [marto]: where are you based robby_dobby?
    [robby_dobby]: marto: Oh, I see you're from an even colder north :-)
    [robby_dobby]: marto: Ldn, right now
    [marto]: icy here today, snow on the hills
    [Corion]: Oh yes, Friday!
    [Corion]: Last day at $work before 3 weeks of vacation (well, not that I'd be really travelling, but no-work-time ;) )
    [robby_dobby]: marto: yeah, I'd eventually visit Glasgow some day - just to visit the home town of GHC :-)
    [robby_dobby]: Corion: Oh, that's awesome! Your timing is perfect enough to see all hell break loose when you get back at work :P
    [marto]: well, let me know in advance, I'll buy you a pint :)

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (10)
    As of 2017-12-15 10:33 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (430 votes). Check out past polls.