Code Comments
Programming Forum and web based access to our favorite programming groups.>>>>> "DB" == Danny Brian <danny@brians.org> writes: DB> Tied hashes a la Regexp::Common. i don't consider tied hashes to be a common use of hashes. and exploring tie will be way beyond the scope of this lesson anyhow. uri -- Uri Guttman ------ uri@stemsystems.com -------- [url]http://www.stemsystems.com[/url ] --Perl Consulting, Stem Development, Systems Architecture, Design and Coding - Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url ]
Post Follow-up to this messageUri Guttman writes:
>
> AP> * Jerrad Pierce <belg4mit@MIT.EDU> [2007-11-23 22:50]:
>
> AP> ( $dispatch{$sub} || sub { warn "no such action '$sub'" } )->();
>
> some variations on that:
>
> my $sub = $dispatch{$key} or die "trying to call missing code" ;
> $sub->() ;
>
> or:
[...]
> or:
>
> my $sub = $dispatch{ $key } || $dispatch{ 'default' } ;
Why stop there? Assuming $key never evaluates to 0:
my $sub = $dispatch{ $key || 'default' };
If it does, wait until 5.10 comes out and:
my $sub = $dispatch{ $key // 'default' };
Although there really is little point stuffing the coderef into a
scalar, it's not like there's anything you can do to it. It's clearer to
not draw attention to it and just run the damned thing:
$dispatch{ $key || 'default' }->();
David
Post Follow-up to this messageOn 23 Nov 2007 at 23:54, Uri Guttman wrote: > > SHC> Vladi Belperchinov-Shabanski wrote: > > SHC> And you wonder why I dislike dispatch tables? So simple in > SHC> concept, so ugly in reality. > > s/dispatch tables/inheritance/g ; > > :) s/inheritance/destructors/g ; :o) /Bernie\ -- Bernie Cosell Fantasy Farm Fibers mailto:bernie@fantasyfarm.com Pearisburg, VA --> Too many people, too few sheep <--
Post Follow-up to this message* David Landgren <david@landgren.net> [2007-11-24 10:45]:
> Uri Guttman writes:
>
> [...]
>
>
> Why stop there? Assuming $key never evaluates to 0:
>
> my $sub = $dispatch{ $key || 'default' };
Your dispatch table has keys for all nonzero strings?
That’s one big hash.
Regards,
--
Aristotle Pagaltzis // <http://plasmasturm.org/>
Post Follow-up to this messageOn Sat, 24 Nov 2007 10:43:58 +0100
David Landgren <david@landgren.net> wrote:
> Uri Guttman writes:
>
> [...]
>
>
>
> Why stop there? Assuming $key never evaluates to 0:
>
> my $sub = $dispatch{ $key || 'default' };
usual case would be there is no value (coderef) for the key, not empty key!
>
> If it does, wait until 5.10 comes out and:
>
> my $sub = $dispatch{ $key // 'default' };
>
> Although there really is little point stuffing the coderef into a
little? really? I'd rather handle situation when $sub is empty.
shuldn't happen or get logged (debug obviously) for example.
> scalar, it's not like there's anything you can do to it. It's clearer to
> not draw attention to it and just run the damned thing:
>
> $dispatch{ $key || 'default' }->();
>
Which still may trigger "Can't use string ("") as a subroutine ref while "st
rict refs" in use at ..."
:)
> David
>
--
Vladi Belperchinov-Shabanski <cade@biscom.net> <cade@datamax.bg>
http://cade.datamax.bg/ pgp/gpg key id: 6F35B214 (pgp.mit.edu)
--
equally destructive as we are, don't you think we've also gone too far?!
Post Follow-up to this message>>>>> "DL" == David Landgren <david@landgren.net> writes:
DL> Uri Guttman writes:
AP> * Jerrad Pierce <belg4mit@MIT.EDU> [2007-11-23 22:50]:
AP> ( $dispatch{$sub} || sub { warn "no such action '$sub'" }
DL> Why stop there? Assuming $key never evaluates to 0:
DL> my $sub = $dispatch{ $key || 'default' };
and what if $key is true but not found?? that is a different
problem. your code doesn't handle that, it only handles a false or missing k
ey
DL> If it does, wait until 5.10 comes out and:
DL> my $sub = $dispatch{ $key // 'default' };
same problem.
DL> Although there really is little point stuffing the coderef into a
DL> scalar, it's not like there's anything you can do to it. It's clearer to
DL> not draw attention to it and just run the damned thing:
DL> $dispatch{ $key || 'default' }->();
there are places where you want to delay the call. you may need to pass
the code ref around or call it multiple times or with different args. i
have found that getting the sub is better as a separate step from
calling it.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- [url]http://www.stemsystems.com[/url
]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding
-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url
]
Post Follow-up to this messageUri Guttman writes:
> DL> Why stop there? Assuming $key never evaluates to 0:
>
> DL> my $sub = $dispatch{ $key || 'default' };
>
> and what if $key is true but not found?? that is a different
> problem. your code doesn't handle that, it only handles a false or missing
key
>
> DL> If it does, wait until 5.10 comes out and:
>
> DL> my $sub = $dispatch{ $key // 'default' };
>
> same problem.
>
> DL> Although there really is little point stuffing the coderef into a
> DL> scalar, it's not like there's anything you can do to it. It's cleare
r to
> DL> not draw attention to it and just run the damned thing:
>
> DL> $dispatch{ $key || 'default' }->();
>
> there are places where you want to delay the call. you may need to pass
> the code ref around or call it multiple times or with different args. i
> have found that getting the sub is better as a separate step from
> calling it.
I see what you and Aristotle are saying. In my own code I arrange things
so that you're never actually feeding random strings into the dispatch
table, I make sure the values are restricted to a finite domain
beforehand, so I can't say I've been bitten by that.
And point taken re: calling it multiple times.
David
Post Follow-up to this message>>>>> "DL" == David Landgren <david@landgren.net> writes:
DL> Although there really is little point stuffing the coderef into a
DL> scalar, it's not like there's anything you can do to it. It's clearer to
DL> not draw attention to it and just run the damned thing:
DL> $dispatch{ $key || 'default' }->();
DL> I see what you and Aristotle are saying. In my own code I arrange things
DL> so that you're never actually feeding random strings into the dispatch
DL> table, I make sure the values are restricted to a finite domain
DL> beforehand, so I can't say I've been bitten by that.
and how do you do that restricting? sounds like a hash is needed there
too! :) and with outside input as in web you need to do this checking in
the server. if you know the data is clean then you can default on a
missing key but otherwise you need to check the key against the dispatch
table.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- [url]http://www.stemsystems.com[/url
]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding
-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url
]
Post Follow-up to this messageI don't think this has been mentioned, although it's really just a
combination of two existing ones: I often have a table of sets.
for my $user (@filenames) {
open(my $fh, "<", $user) {
while(<$fh> ) {
$use{$user}{$1} = 1 while /include\s*['"](.*?)['"]/g;
}
}
It's a full graph, with fast looking and iteration over outgoing edges.
The quick existence lookup is nice while building it, but if you don't
care about that, then it's cleaner to encode it as a map from node to
array ref of children:
$children{A} = [ 'B', 'C', 'D' ];
$children{C} = [ 'E', 'F' ];
..etc...
There's sort of another use buried in there, too: hashes can be used
to map object identifiers to their data (a la inside-out objects or
something simpler that is just trying to keep the real data in a
canonical place, perhaps to avoid worrying about cyclic references.)
For an obscure and rather unsafe usage, you could use hashes for randomizati
on:
my @quiz_questions = ...;
my %quiz_table;
@quiz_table{@quiz_questions} = ();
my ($random_first_question) = keys %quiz_table;
while my $question (keys %quiz_table) {
print "QUESTION: $question ";
my $answer = <STDIN>;
..;
}
Hashes can be used for caches or memoization:
our $answer;
sub func {
return $answer ||= ...compute...;
}
Hashes can be used for sparse arrays:
$arr{10} = "X";
$arr{83719} = "Y";
Hashes can be used for symbol tables. :-)
Post Follow-up to this message>>>>> "SF" == Steve Fink <sphink@gmail.com> writes:
SF> I don't think this has been mentioned, although it's really just a
SF> combination of two existing ones: I often have a table of sets.
SF> for my $user (@filenames) {
SF> open(my $fh, "<", $user) {
SF> while(<$fh> ) {
SF> $use{$user}{$1} = 1 while /include\s*['"](.*?)['"]/g;
SF> }
SF> }
SF> It's a full graph, with fast looking and iteration over outgoing edges.
i consider graphs a variant of deep (and cyclical) data structures. and
i think they are more complex than the other simple hash uses. also
graphs are too hard to explain in one slide for this class. but i will
keep it in mind as another use. all the classic linked list structures
can be easily done with hashes for sure.
SF> There's sort of another use buried in there, too: hashes can be
SF> used to map object identifiers to their data (a la inside-out
SF> objects or something simpler that is just trying to keep the real
SF> data in a canonical place, perhaps to avoid worrying about cyclic
SF> references.)
i use a hash to track all objects created by a class. the fun part is
that the key and value were the same thing. but the key gets stringified
which is why you still need the ref/object as the value.
SF> Hashes can be used for caches or memoization:
SF> our $answer;
SF> sub func {
SF> return $answer ||= ...compute...;
SF> }
caching is a good one. that is all that memoize uses.
SF> Hashes can be used for sparse arrays:
SF> $arr{10} = "X";
SF> $arr{83719} = "Y";
i mentioned that one.
SF> Hashes can be used for symbol tables. :-)
true. i didn't mention that. but i always teach to use hashes for data
and not the symbol table (via symrefs). mung the symbol table only when
you need to mung it!
thanx,
uri
--
Uri Guttman ------ uri@stemsystems.com -------- [url]http://www.stemsystems.com[/url
]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding
-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url
]
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.