Code Comments
Programming Forum and web based access to our favorite programming groups.On Friday, Nov 23rd 2007 at 14:53 -0000, quoth Jerrad Pierce:
=>>I admit I'm better with python these days than I am with perl, but one of
=>>the caveats that the dispatch table should be taught with is that
=>>selecting the correct code ref is different from calling through it.
=>
=>You might want to explain yourself in fuller, properly punctuated,
=>detail there (particularly since it seems to not be about hashes per se)
Sorry, I thought I was clear and properly punctuated.
Given:
table = { ... }
eg
ref = table{ expr }
ref()
is different from
table{expr}()
The latter can be a bear to debug ;-)
In the second case, you're calling a function which can produce any error
that all of the functions in table are capable of generating. This may
also include an illegal reference into table in the first place. In the
first example, you eliminate the indexing into the dispatch table as a
seperate step from actually making a subroutine call.
--
Time flies like the wind. Fruit flies like a banana. Stranger things have .
0.
happened but none stranger than this. Does your driver's license say Organ .
.0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 0
00
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
Post Follow-up to this message>Sorry, I thought I was clear and properly punctuated.
It was a dig at your use of the sigil-less language in your example :-P
>In the second case, you're calling a function which can produce any error
>that all of the functions in table are capable of generating.
I have no idea what you mean by this, are you sure you're not carrying over
some freaky Python conventions to Perl?
>This may also include an illegal reference into table in the first place.
Yes, and?
>In the first example, you eliminate the indexing into the dispatch table as
a
>seperate step from actually making a subroutine call.
That's an phrase. Regardless, your proposal still would not solve the proble
m
of using a non-existent subruotine, for that you'd need:
exists( $dispatch{$sub} ) ? $dispatch{$sub}->() :
warn "Key <$sub> does not exist in the dispatch table";
--
Free map of local environmental resources: http://CambridgeMA.GreenMap.org
--
MOTD on Boomtime, the 35th of The Aftermath, in the YOLD 3173:
You don't need a Swiss Army knife to cut yourself, a piece of paper will do
just fine. --JP
Post Follow-up to this message* Jerrad Pierce <belg4mit@MIT.EDU> [2007-11-23 22:50]:
> exists( $dispatch{$sub} ) ? $dispatch{$sub}->() :
> warn "Key <$sub> does not exist in the dispatch table";
( $dispatch{$sub} || sub { warn "no such action '$sub'" } )->();
--
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1
}
&Just->another->Perl->hack;
#Aristotle Pagaltzis // <http://plasmasturm.org/>
Post Follow-up to this message* Mr. Shawn H. Corey <shawnhcorey@magma.ca> [2007-11-24 00:50]:
> my $sub = ( exists $dispatch{ $key } && ref( $dispatch{ $key } ) eq 'CODE'
)
> ? $dispatch{ $key }
> : $dispatch{ 'default' };
>
> Just because you're not paranoid doesn't mean computers don't
> hate you :)
So why did you skip the check for whether the default value is
a CODE ref?
Regards,
--
Aristotle Pagaltzis // <http://plasmasturm.org/>
Post Follow-up to this messageA. Pagaltzis wrote: > * Mr. Shawn H. Corey <shawnhcorey@magma.ca> [2007-11-24 00:50]: > > So why did you skip the check for whether the default value is > a CODE ref? Because I don't like dispatch tables. When I see them I want to convert the whole thing to objects and starting thinking about inheritance and polymorp hism. Which will take care of such problems at "compile" time. But this thread is about hashes not objects. (And I still think computers hate me.) -- Just my 0.00000002 million dollars worth, Shawn +------------\ | Shangri La \ | 40,000 km / +------------/
Post Follow-up to this messageOn Fri, 23 Nov 2007 23:18:20 +0100
"A. Pagaltzis" <pagaltzis@gmx.de> wrote:
> * Jerrad Pierce <belg4mit@MIT.EDU> [2007-11-23 22:50]:
>
> ( $dispatch{$sub} || sub { warn "no such action '$sub'" } )->();
>
or &{ ... || ... }->()
just to avoid problems like:
print ( $dispatch{$sub} || sub { warn } )->();
--
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 messageVladi Belperchinov-Shabanski wrote:
> On Fri, 23 Nov 2007 23:18:20 +0100
> "A. Pagaltzis" <pagaltzis@gmx.de> wrote:
>
>
> or &{ ... || ... }->()
>
> just to avoid problems like:
>
> print ( $dispatch{$sub} || sub { warn } )->();
And you wonder why I dislike dispatch tables? So simple in concept, so ugly
in reality.
--
Just my 0.00000002 million dollars worth,
Shawn
+------------\
| Shangri La \
| 40,000 km /
+------------/
Post Follow-up to this message>>>>> "SHC" == Shawn H Corey <shawnhcorey@magma.ca> writes: SHC> A. Pagaltzis wrote: SHC> Because I don't like dispatch tables. When I see them I want to SHC> convert the whole thing to objects and starting thinking about SHC> inheritance and polymorphism. Which will take care of such SHC> problems at "compile" time. and how would an object remove the need for a dispatch table? you can't always map keys to methods and OO isn't needed in all cases. you can also do dispatch tables inside a class and again much simpler than OO or polymorphism. inheritance is right out as it is baggage in most designs. people think OO eq inheritance and that is so wrong. i prefer message passing and its flavor of polymorphism any day over inheritance and its rigid class hierarchies and dependency trees. SHC> But this thread is about hashes not objects. SHC> (And I still think computers hate me.) not as much as i hate (but have used) inheritance. i should start a side thread for this: ever write an OO system where one class ISA and HASA another class at the same time? i have and for a good reason and it works so elegantly. :) 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>>>>> "SHC" == Shawn H Corey <shawnhcorey@magma.ca> writes: 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 ; :) 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.