in reply to WWW::ImagesHack

I second podmaster's onte about WWW::Mechanize -- it can make code much simpler..

But what i wanted to note is that you could reduce the duplicate code and make maintenance (and use) a little easier by just having one accessor .. (there's many ways to do this -- here's just a quick one)
our KEY_LIST => [ qw/ thumbnailForWebsite thumbnailForForums1 thumbnailForForums2 hotlinkForForums1 hotlinkForForums2 hotlinkForWebsite showImage directLinkToImage / ]; sub get { my $self = shift; my $key = shift; # could grep @{&KEY_LIST} here too to check for valid key return exists $self->{liste}->{$key} ? $self->{liste}->{$key} : unde +f; } # this could be left the same or changed to this: sub get_all { my $self = shift; return { map { $_ => $self->{liste}->{$_} } @{&KEY_LIST} }; } # note: could also replace get_all() with get() or get('all') # and note that the loop in upload() becomes just: foreach( @{&KEY_LIST} ){ ... }
Use of the module then becomes:
print $ih->get('thumbnailForWebsite'), "\n"; print $ih->get('thumbnailForForums1'), "\n"; print $ih->get('thumbnailForForums2'), "\n"; print $ih->get('hotlinkForForums1'), "\n"; print $ih->get('hotlinkForForums2'), "\n"; print $ih->get('hotlinkForWebsite'), "\n"; print $ih->get('showImage'), "\n"; print $ih->get('directLinkToImage'), "\n";

Replies are listed 'Best First'.
Re^2: WWW::ImagesHack
by Pied (Monk) on Sep 15, 2005 at 15:51 UTC
    Yes thanks. I'll definitely use WWW::Mechanize. Ihad been searching CPAN for stuff like that but couldn't find it (as I had never used it, didn't really know what to look for).

    As for the refactoring, yes, this is a good idea, though what I was thinking of was rather dynamic sub creation, something like
    foreach(@keylist){ my $key = shift; sub get_$key { #code of the func} push @funcs,\get_$key; }
    But if this is possible, then I should return the subs' references so that they aren't destroyed when I loop in the foreach...and then export them all dereferenced...
    How about that? Or is it obfuscating the code for peanuts?


    P!

    Are the monkeys gone yet?
      i meant to mention AUTOLOAD as another way to do it dymanically ... check out the "AUTOLOAD: Proxy Methods" section in perltoot -- i think it's exactly what you're looking for...