OT: CS education gone wrong (Was: Re: TDD is BS?)

Adam D. Ruppe destructionator at gmail.com
Sun Jun 23 17:45:25 PDT 2013


On Sunday, 23 June 2013 at 17:54:01 UTC, Andrei Alexandrescu 
wrote:
> Awesome!

I realized initializing the runtime might have been a mistake 
when loading the .so and moreover I was using the wrong calling 
convention. Fixed that and now the shared library thing works. On 
Linux at least, I hardcoded ".so" so don't try it anywhere else.

Here's some code:
http://arsdnet.net/dcode/dhp.zip

unzip it, hit make. dhp2.d is the main file here, it also uses 
some helper libraries you can see in the arsd folder.

Then run ./dhp2 in another window or something. It is a long 
running process that serves http.

Head over to your browser and surf to
http://localhost:8085/


It will take a couple seconds to load the first time, because it 
needs to compile the code. Subsequent loads will pull it from a 
cache and be faster.

Anyway it will show you some stuff and a link. These pull from 
the files in the zip called index.dhp and dhpt.dhp. index is just 
html, no D code, although it will compile it since this server 
compiles everything.


dhpt.dhp actually includes some D:

         <p>Hello, <% cgi.write(cgi.request("name", "user")); %>, 
happy to see you.</p>

Note: you access that by going to localhost:8085/dhpt, NOT 
/dhpt.dhp. It strips out dots from the url as a way of sanitizing 
the filename so keep it simple.

It uses ASP style <% %> tags instead of <?d ?> because my dom.d 
already understands them. (BTW this parses the .dhp files to be 
well-formed xml, so if you mismatch tags, it will throw. It might 
be fun to put the DOM node in scope to inspect too).

There's a Cgi cgi in scope in the function it builds here. Use it 
to do communication instead of writeln() etc., as seen in this 
example.



Here's where the shared library magic comes in: feel free to edit 
one of those .dhp files, or create your own, and go back to it in 
the browser. It will recompile and present it to you without 
having to restart the server. That's kinda cool. The downside is 
if you segfault in here it will take the whole server down so 
don't do that.

If you fail compiling though, it will actually read dmd's output 
and translate the filename and line number to match the .dhp file 
input, instead of the .d file dmd actually sees. So it feels less 
like a filthy hack then.

Feel free to look at index.d and dhpt.d in that same folder after 
you browse to them to see what the generated code looks like.




Soooo yeah. I'll probably never use this, dom templates rok so 
much more than anything asp/php style, but if you wanna play, 
feel free and let me know if you want more features or see bugs. 
Probably won't be too hard to fix up now that it is started.


More information about the Digitalmars-d mailing list