Re: Using a string as a hash (symbolic ref)



"SB" == Steve Bertrand <steve@xxxxxxxxxx> writes:

SB> In all honesty, I think I'm learning more about Perl (and my own
SB> programs) writing the tests than I do when I'm writing the programs
SB> themselves. (Same goes for keeping up with the documentation!).

coding is coding and the more you do the more you learn. tests are a
particular area but they don't necessarily use different coding
constructs than other projects use.

SB> Given your comments above, would you say that this is better?

SB> my %typedefs = (
SB> plan_info => \&plan_info,
SB> user_info => \&user_info,
SB> plan => \&plan,
SB> ...etc...
SB> );
SB> ...

just so you know, that is called a dispatch table. it is used to lookup
a sub based upon a key. it is used in many sorts of projects. i wouldn't
use the name %typedefs as it doesn't say much about the use of the
table. note that names should reflect how something is used, not what it
contains. maybe type2info is a better name. i still don't get the use of
this so i can't create a proper name for it.

SB> my @known_types = $vardb->is_type();

SB> my @missing_types;
SB> my %data;

SB> for my $known_type (@known_types) {

SB> unless (exists $typedefs{$known_type} ) {

again, you don't need exists there. since your dispatch table's values
are always code refs so they will always be true.

SB> push (@missing_types, $known_type);
SB> next();
SB> }

SB> %data = &{ $typedefs{$known_type} };

you didn't get my point about redundancy. $typedefs{$known_type} is used
twice which is once too many. if you refer to a hash value (especially
deep hashes) more than once, it is usually better to grab it into a
scalar and use that. it will likely be faster (saves hash lookups), it
is shorter (removed duplicate code) and is usually less bug prone as you
one have the expression (which can be complex) in one place.

SB> eval { $sanity->check_type( $known_type, \%data, $error ) } ;

SB> unlike ( $@,

again, what is unlike? i now guess it is from a test module. does it
take a regex as an arg?

SB> '/not a defined datatype/',

that isn't a regex, just a plain string with / chars on the ends. maybe
the unlike call will convert that but the / will be literal / chars. use
qr// or maybe the string without /.

uri

--
Uri Guttman ------ uri@xxxxxxxxxxxxxxx -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
.



Relevant Pages

  • Re: Using a string as a hash (symbolic ref)
    ... SB> programs) writing the tests than I do when I'm writing the programs ... coding is coding and the more you do the more you learn. ... Both unlike and like take a regex as it's ... the unlike call will convert that but the / will be literal / chars. ...
    (perl.beginners)
  • Re: Which programming jobs will not be sent overseas?
    ... That is, of course, a statement of your opinion, and so can hardly be ... > writing is improved by first dealing with my own anger. ... > Requirements writing is more difficult than bonehead coding, ...
    (comp.programming)
  • Re: comparison between portability libraries?
    ... most of these offer functionality that is imo a minor issue to ... glib contains code for all kind of data structures, ... before (writing both the client code and a server). ... a years worth of coding (maybe a year coding on average for about 4 ...
    (comp.lang.misc)
  • Re: 1st ed Request on Stats
    ... a software upgrade was being done. ... coding in university did help me communicate problems with the ... BTW, I have done substantial professional writing, some professional editing, ... They pay me. ...
    (rec.games.frp.dnd)
  • Re: Yikes!
    ... I too would like to encourage anyone that is still coding for the ... Atari, to take advantage of the work that has been put into FreeMiNT ... and 15-years-old programming tutorials do not ... who wrote it are either gone or still writing for it. ...
    (comp.sys.atari.st)