Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: RFC on how I'm doing when it comes to writing objects?

by tmharish (Friar)
on Feb 05, 2013 at 07:04 UTC ( [id://1017074]=note: print w/replies, xml ) Need Help??


in reply to Re^2: RFC on how I'm doing when it comes to writing objects?
in thread RFC on how I'm doing when it comes to writing objects?

To expand on what mbethke said:

Think of each of your accounts as an entity that is self (pun intended) contained - It has information about that account stored in it and has methods that know what to do with that information.

Each of these entities could be an object, the template for which is defined by the class that you write like so:

package My::Twitter::Account; use strict ; use warnings ; .... sub new { my $class = shift; my %parameter_hash; my $count = @_; %parameter_hash = @_; croak( "MISSING FILE : \n " . $useage_howto ) unless( $ +parameter_hash{ FILE } ) ; croak( "MISSING HEADINGS: \n " . $useage_howto ) unless( $ +parameter_hash{ HEADINGS } ) ; croak( "MISSING NAME : \n " . $useage_howto ) unless( $ +parameter_hash{ NAME } ) ; $parameter_hash{ DEBUG } = 0 unless( $parameter_hash{ DEBUG } ); my $self = { FILE => $parameter_hash{ FILE } , HERADINGS => $parameter_hash{ HEADINGS } , NAME => $parameter_hash{ NAME } , SOME_USEFUL_INFO_MAYBE => $other_useful_info , DEBUG => $parameter_hash{ DEBUG } , } bless( $self, $class ); dump( $self ) if( $self->{DEBUG} == 1 ); return $self; } ... Other Methods that deal with a SINGLE twitter account here.

Now that we have something to deal with a single account we can combine these either into a second package or decide to use them in a non-OO way. Lets do the First:

package My::Twitter::AllAccounts; use strict ; use warnings ; ... blah blah blah sub new { my $class = shift; my %parameter_hash; $parameter_hash{ DEBUG } = 0 unless( $parameter_hash{ DEBUG } ); my @twitter_accounts ; my $self = { TWITTER_ACCOUNTS => \@twitter_accounts , DEBUG => $parameter_hash{ DEBUG } , } bless( $self, $class ); dump( $self ) if( $self->{DEBUG} == 1 ); return $self; } sub add_twitter_account { my $self = shift ; my $name = shift ; my ( $file, $headings ) = _function_that_gets_files_and_headi +ngs(); # This can also be inside the Twitter::Account class. my $twitter_account = new My::Twitter::Account( NAME => $name , FILE => $file , HEADINGS => $headings , ); push( @{ $self->{ TWITTER_ACCOUNTS } }, $twitter_account ) ; return 1; }

Now we have a way to store a single twitter account and a way to store the collection we can do things like:

my $twitter_data = new My::Twitter::AllAccounts() ; my @accounts = qw( Lady_Aleena LadyAleena_ABC LadyAleena_CBS LadyAleena_FOX LadyAleena_NBC LadyAleena_SyFy LadyAleena_TNT LadyAleena_USA LadyAleena_TV LadyAleena_eros LadyAleena_home LadyAleena_test ); foreach my $account ( @accounts ) { $twitter_data->add_twitter_account( $account ); }

Of course the really fun part begins now:

# Am assuming you move your functions into the correct classes and wri +te some ... But the below is just for illustration. $twitter_data->reload_account_data( $account_name ); $twitter_data->get_sum_of_all_accounts(); $twitter_data->remove_twitter_account( $account ); $twitter_data->temporarily_ignore_accounts( [ account1, account2 ] ); $twitter_data->get_sum_of_all_accounts(); $twitter_data->stop_ignoring();

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1017074]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-03-19 07:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found