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 mbethke (Hermit)
on Feb 05, 2013 at 04:58 UTC ( [id://1017061]=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?

Comments are in. Hope you find them helpful in pointing me in the right direction. I tested the 2 argument bless in the first subroutine. When it was data dumped, I didn't see any change in how the data was stored.

It makes no difference now, nor would it matter with the way you use the returned values. But it will cause you problems later once you start having a proper class hierarchy because it breaks inheritance. Just forget the single-argument blessexists, it's no good.

I got the same result without $class on the last line. When I use the subroutine as an object, I would do something like...
my $accounts = Twitter::Objects->accounts; for my $account (@{$accounts}) { ... }

That's not using it as an object, that's a plain old arrayref that works the same whether or not you bless it.

As 7stud and others have pointed out, the basic problem is that you haven't organized your code in an OO way so "objectifying" it doesn't work like that. You first have to identify what the "things" are in the system you're building, and what properties they have. "Accounts" would probably be such a thing---a collection of account objects. You'd pass its constructor, "new() usually, a directory that it reads to determine what the accounts are called. If stuff lives in Twitter/$account/... you can generalize that and don't have to hardcode the list. Remember the whole OO dance is supposed to help with code reuse and if the code contains your account list it's not reusable at all. So the instance data would be e.g. a directory and a list of names. Then you could ask the Accounts collection for a list of account names and for an object representing a named account. The Account class wouldn't have to know that its configuration lives in a certain directory hierarchy, it just gets passed a directory by the Accounts collection and reads its files from there. "Lists" would probably be a class of its own that somehow deals with an Accounts object, though I don't understand well enough what a "list" is in this context.

Playing with the code from perlboot should give you a better understanding of how to change your code organization so it actually benefits from OOP.

Replies are listed 'Best First'.
Re^4: RFC on how I'm doing when it comes to writing objects?
by Lady_Aleena (Priest) on Feb 05, 2013 at 19:38 UTC

    Since I am using these subroutines to return arrays and hashes, they do not benefit from being made into something which looks like an object. I should rename the module to Twitter::Data, take out all of the $class variables and bless uses, and change the calls to it from Twitter::Objects->subName; to Twitter::Data::subName;. The module will still be useful since I will not have to copy and paste the same arrays and hashes in the thirty-odd scripts I have written using the data. Thank you for helping me see where I went wrong in my thinking.

    Have a cookie and a very nice day!
    Lady Aleena
      Sure it's useful to use this to avoid copy&paste coding, but you might learn a lot if you actually refactored it into OO style. Of course it will take longer; depends whether you need something to run tomorrowish or it's more a learning project.

        Maybe if I showed some of the scripts where the data is used, maybe some pointers can be given on where my mind should be if I decide to try again with OO for the data.

        list_links.pl

        list_links sends HTML list items <li> to the clip board so I can paste them into a Tumblr post I update every once in a while to get more people to subscribe.

        #!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use Lingua::EN::Inflect qw(NO); use Win32::Clipboard; use lib '../files/perl/lib'; use Twitter::ListSort qw(list_compare); use Twitter::Data; my %lists = Twitter::Data::all_lists; # This is all of the lists I kee +p across all my accounts. my $board; for my $list (sort { list_compare($a,$b) } values %lists) { # Starting + the loop. next if !grep(/owner/,@{$list->{status}}); my $targ = $list->{target}; my $link = $list->{link}; my $name = length($list->{long_name}) ? $list->{long_name} : $list-> +{name}; my $targ_text = $targ > 0 ? " (".NO('subscriber',$targ)." 'til targe +t met)" : ''; my $asterisk = $list->{user} eq "Lady_Aleena" ? '*' : ''; $board .= qq(<li><a href="$link">$name</a>$asterisk$targ_text</li>\n +); } my $clip = Win32::Clipboard(); $clip->Set($board);

        list_people.pl

        list_people does three things.

        1. It tells me who I have not made members of my List subscribers list. Anyone who subscribes to one of my lists becomes a member of List subscribers. I take this group and add them to List subscribers with another script. Sometimes removed or suspended Twitter accounts remain as subscribers of a list for months or even years. I check to make sure the account exists before trying to add it (or readd it) to the List subscribers list.
        2. It tells me who is no longer subscribed to my lists who still remains on the List subscribers list. There is a way with Net::Twitter::Lite to remove people from the list programmatically, I just have not added it to the script yet.
        3. Since most of my accounts (not my main Lady_Aleena account) were created to house lists, most (if not all) of the people followed by the account is either a member or subscriber to a list. This section checks to see if the account is following someone who is neither a list member or list subscriber. The first time I ran the script with this section, I ended up creating about eight new lists.
        #!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use Lingua::EN::Inflect qw(WORDLIST); use Text::Table; use Win32::Clipboard; use lib '../files/perl/lib'; use Twitter::Data; my @accounts = Twitter::Data::accounts; my %lists = Twitter::Data::all_lists; my %lists_people = Twitter::Data::lists_people; my @subscribers = @{$lists{'72710881'}{members}}; local $\ = "\n"; # 1 above. print 'Subscribers not on the list'; for my $subscriber (keys %{$lists_people{subscribers}}) { my $s_lists = WORDLIST(@{$lists_people{subscribers}{$subscriber}}, { +conj=>''}); print "$subscriber - $s_lists" if !grep($_ eq $subscriber,@subscribe +rs); } print "\n"; # 2 above. print 'Non-subscribers on the list'; for my $subscriber (@subscribers) { print $subscriber if !$lists_people{subscribers}{$subscriber}; } print "\n"; # 3 above print 'People followed but not listed (by account)'; for my $account (@accounts) { next if $account =~ /^(LadyAleena_(?:eros|test))$/; my %account_people = Twitter::Data::acct_data($account,'people'); my @not_members; for my $person (values %account_people) { my $name = $person->{screen_name}; push @not_members, $name if ($person->{friends} == 1 && !$lists_pe +ople{members}{$name}); } local $\ = "\n"; if (scalar(@not_members) > 0) { print "$account"; for (sort {lc $a cmp lc $b} @not_members) { $board .= "\t$_\n"; print "\t$_"; } } }

        If you see anything in there where OO would come in handy, let me know. These two scripts are probably the shortest of those I have written where I need the data.

        Have a cookie and a very nice day!
        Lady Aleena

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-03-19 04:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found