WWW::UsePerl::Journal

Tony Bowden tony at kasei.com
Sun Jan 27 13:37:39 CST 2002


On Sun, Jan 27, 2002 at 06:04:30PM +0000, Russell Matbouli wrote:
> http://testers.cpan.org/search?request=dist&dist=WWW-UsePerl-Journal
> The thing is that I don't know if they were caused just because I didn't 
> write a good test harness, or if there is an actual bug in my program. 
> Anyone care to enlighten me?

Seems to be some of each :)

Firstly, you don't really need tests like:

  isnt($UID, undef, "getUID returns defined");
  isnt($UID, "", "getUID doesn't return empty string");
  is($UID, 1413, "getUID gets the right ID");

The third test implies the first 2 anyway ... if it's 1413, then it
can't be undef or the empty string ...

Then 
  my @entries = $j->getEntryList($username);
  isnt($#entries, 0, "getEntryList got more than one entry");

isn't doing what you think. 
If @entries is empty, as it is when I run the test, $#entries is -1.
You can just use:

  isnt (scalar @entries, 0, "getEntryList got more than one entry");

It seems that this array is empty because your pattern match is wrong
when scanning the list of all journal entries. I think the journal urls
have changed: they're now like
  http://use.perl.org/~russell/journal/2340/

> The error seems to be thrown from Test::Builder, but afaik I'm using
> Test::More. What's going on here, other than my lack of knowledge of the
> test harness being shown...?

Test::More uses Test::Builder. You can blame Schwern on the confusing
error messages ;)


As regards the module itself - every 'method' takes an initial argument
of the username ... so you may wish to consider making it part of the
constructor instead...

Then you could have an interface like:

  use WWW::UsePerl::Journal;
  my $j = WWW::UsePerl::Journal->new("russell")
  my %journal = $j->journal_entries;

  foreach (keys %journal) {
    my $content = $j->fetch($_);
   # or
    my $content = $journal->fetch_by_name($j{$_})
  }

The guts of this would be something like:

  use strict;
  use vars qw($VERSION);
  use LWP::Simple;
  
  use constant UP_URL => 'http://use.perl.org';
  
  $VERSION = '0.02';
  
  sub new {
      my $class = shift;
      my $user  = shift or die "We need a user";
      my $self  = bless { user => $user }, $class;
      return $self;
  }
  
  sub user { shift->{user} }
  
  sub uid {
      my $self = shift;
      $self->{_uid} ||= do {
        my $user = $self->user;
        my $content = get(UP_URL . "/~$user/")
          or die "Cannot connect to Use Perl";
        $content =~ /User Info for $user \((\d+)\)/ism
          or die "$user does not exist";
        $1;
      }
  }
  
  sub journal_entries {
      my $self = shift;
      $self->{_entries} ||= do {
        my $UID = $self->uid;
        my $content = get(UP_URL . "/journal.pl?op=list&uid=" .  $self->uid)
          or die "Cannot fetch journal entry list";
        [ map m#/journal/(\d+)/"><B>(.*?)</B></A>#, split /\n/, $content ];
      };
      return @{$self->{_entries}};
  }
  

I'll leave fetch as an exercise ;)

Tony




More information about the Belfast-pm mailing list