in reply to Clean perl code (unused functions)

Consider:

use strict; use warnings; my $obj = bless {}; my @mem = qw(hello world); for (@mem) { my $mem = "_do_$_"; next unless $obj->can ($mem); $obj->$mem (); } sub _do_hello {print 'hello ';} sub _do_world {print "world\n";} sub _do_nothing {print 'nothing ';}

How could a mechanical parser decide what to keep and what to clean? How about when the contents of @mem depends on a preceding parse step and thus on user input?

It may be that tools such as UML::Class::Simple will help. Becoming familiar with a too-big codebase? and Analyzing large Perl code base. have good answers that may help too.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Clean perl code (unused functions)
by Errto (Vicar) on Jun 25, 2007 at 19:27 UTC
    Not exactly related to the OP, but I am wondering: how does that code pass strict? Isn't it effectively using symbolic references, ie. generating a method name from a string?

      I've trawled through much of the Perl documentation looking for something that addresses exactly that issue and can't find anything. Pretty much all of the pertinent examples use scalar variables rather than member functions.

      It is however a very useful technique to dispatch to members in the context of a parser. Decorate the verb to avoid the possibility of nefarious calling of unintended members (DESTROY in the document being parsed could otherwise be a little unfortunate) and you've got a very easily extensible interpreter for all sorts of stuff (see sub describeAtom in Updated QuickTime format movie file dumper for example).

      It's probably better to use the function reference returned from can (see 'UNIVERSAL: The Root of All Objects' in perltoot) though (amazing what you learn by re-reading the docs;) ):

      for (@mem) { next unless my $member = $obj->can ("_do_$_"); $obj->$member (); }

      DWIM is Perl's answer to Gödel