Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Re: fun with hashes!
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

Report this thread to moderator Post Follow-up to this message
Old Post
Steven W. Orr
11-24-07 12:29 AM


Re: fun with hashes!
>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

Report this thread to moderator Post Follow-up to this message
Old Post
Jerrad Pierce
11-24-07 12:29 AM


Re: fun with hashes!
* 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/>

Report this thread to moderator Post Follow-up to this message
Old Post
A. Pagaltzis
11-24-07 12:29 AM


Re: fun with hashes!
* 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/>

Report this thread to moderator Post Follow-up to this message
Old Post
A. Pagaltzis
11-24-07 12:29 AM


Re: fun with hashes!
A. 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   /
+------------/

Report this thread to moderator Post Follow-up to this message
Old Post
Mr. Shawn H. Corey
11-24-07 12:29 AM


Re: fun with hashes!
On 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?!

Report this thread to moderator Post Follow-up to this message
Old Post
Vladi Belperchinov-Shabanski
11-24-07 03:29 AM


Re: fun with hashes!
Vladi 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   /
+------------/

Report this thread to moderator Post Follow-up to this message
Old Post
Mr. Shawn H. Corey
11-24-07 03:29 AM


Re: fun with hashes!
Tied hashes a la Regexp::Common.


Report this thread to moderator Post Follow-up to this message
Old Post
Danny Brian
11-24-07 03:29 AM


Re: fun with hashes!
>>>>> "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
]

Report this thread to moderator Post Follow-up to this message
Old Post
Uri Guttman
11-24-07 09:49 AM


Re: fun with hashes!
>>>>> "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
]

Report this thread to moderator Post Follow-up to this message
Old Post
Uri Guttman
11-24-07 09:49 AM


Sponsored Links




Last Thread Next Thread Next
Pages (5): « 1 [2] 3 4 5 »
Search this forum -> 
Post New Thread

PERL Tricks archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:46 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.