sdbarker has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Why CGI::Application?
by dragonchild (Archbishop) on Jan 13, 2004 at 13:10 UTC | |
Also, you might be confusing something I was having issues with, at first. I originally thought I needed to implement my entire application as one monolithic C::A app. But, a friend of mine showed me what his company did. They have some 20 different C::A's that all work together, passing responsability off as necessary. They have one main C::A, which handles logging and the homepage. Then, every major subsystem (reports, user admin, preferences, etc) have their own C::A. If something is complicated, create another C::A inside that. His rule of thumb was 10-12 states, at most. More than that and you should look at breaking it up into two C::A's (if possible). Also, every one of your C::A's inherits from some abstract superclass that inherits from C::A, which implements things like how to connect to the DB, what CGI class to use, how to display, and other basic functionality. ------
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified. | [reply] [d/l] |
by Arunbear (Prior) on Jan 13, 2004 at 14:06 UTC | |
| [reply] |
by dragonchild (Archbishop) on Jan 13, 2004 at 18:12 UTC | |
Read more... (2 kB)
Ok, so far so good. But, we don't have another C::A yet. I personally do my navigation through some header that I've TMPL_INCLUDE'd into all the relevant templates. (All of them, except login and logout.) The header basically has
But, you wanted to see the other C::A. Here ya go: Read more... (660 Bytes)
That's it. No handling of cookies, no handling of nothing. In fact, there's absolutely no way the cookies are NOT going to be handled. You can't forget to add it to the top. You can't forget to do standard processing. It's quite ... nice to be able to forget that you have it cause it just works. ------
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified. | [reply] [d/l] [select] |
by t'mo (Pilgrim) on Jan 14, 2004 at 19:58 UTC | |
by dragonchild (Archbishop) on Jan 14, 2004 at 20:56 UTC | |
by wfsp (Abbot) on Apr 27, 2007 at 10:23 UTC | |
by dragonchild (Archbishop) on May 01, 2007 at 18:22 UTC | |
|
Re: Why CGI::Application?
by rob_au (Abbot) on Jan 13, 2004 at 12:00 UTC | |
Additionally, and most importantly, by providing a framework in which CGI parameter parsing, template handling and session storage can be seamlessly incorporated, CGI::Application allows developers to focus on business logic rather than framework implementation details.
perl -le "print unpack'N', pack'B32', '00000000000000000000001010101010'" | [reply] |
by kropotkin (Initiate) on Jan 31, 2009 at 18:13 UTC | |
The author of that text hasn't used Mason very much. If you wanted with Mason you could put all your components in one file using the inline component block - or you could just use one component and wite a great big MVC framework if you wanted to. I agree 100% with the author of the original comment. The problem with a lightweight MVC framework like C:A is: a) It often adds an extra layer (to be learned and more code to load) for very little obvious benefit. E.g. to use JSON there is a JSON plugin, but this is not much more than a wrapper around the JSON module. In as much as it is more it provides a mechanism for intercepting the output in your C:A application with a hook. My problem with this is if I wanted this kind of functionality I'd rather code it myself than learn how someone else has done it. b) Because it is lightweight your application will end up as a hybrid beteen C:A and your own systems e.g. I've had to develop a little component system because it doesn't really do components, being stuck in this Web 1 way of thinking where the whole page is served each time. But this is messy. There are two ways out of this: either use a framework which really does cover everything, like PHP's Symfony (if that's your bag), or don't use a framework at all. The MVC model is fine if appropriate: but most developers should have the discipline to separate out model view and controller without being put into a strait-jacket. Justin | [reply] |
|
Re: Why CGI::Application?
by derby (Abbot) on Jan 13, 2004 at 13:32 UTC | |
It seems as though all of the benefits of C::A are things that can be accomplished by simply adjusting your coding style; making more blocks of code into subroutines, etc.
Sure that will work. Hey but wait, darnit, I want to use CGI::Simple instead of CGI ... better write that hook. Hey but wait, darnit, I want to do some post-processing after the HTML is sent to the client ... better write that hook. Hey but wait, darnit, I need to get into the process after reading the HTTP stream but before I do my real app stuff ... better write that hook. Hey but wait, darnit, I need a hook after generating content but before sending the headers ... better write that hook. Hey but wait, darnit, I need a hook to modify the headers ... Hey but wait, darnit, I need a hook to override what step is being executed ... Hey but wait ... CGI::Application gives all this to me and it's been developed, reviewed and used by a slew of talented and qualified people. You see the authors have given you a framework that makes the easy things easy and the hard things possible. Sure you could do that yourself, but you could also write CGI.pm yourself or for that matter your own interpreter. To me, CGI::Application did not add a layer of complexity and confusion but actually cleared up a lot of issues when trying to build MVC apps. I always knew templates were the VIEW and application specific modules were the MODEL ... it was that damn CONTROLLER that became a big hairy beast ... well CGI::Application really cleaned up my thought process when it came to MVC. So sure I could have spent weeks, months, years creating my own Application framework ... one that's easy to use for the straightforward cases but also allows me to hook in and change things on the fly or I could spend 15 minutes downloading CGI::Application. So I say well done Jesse (and all the contributors). -derby | [reply] |
|
Re: Why CGI::Application?
by jeffa (Bishop) on Jan 13, 2004 at 17:03 UTC | |
Maybe if you understood where C::A came from, you might understand why we use it. In the beginning there was the very large if elsif ... else structure of blocks. It worked, and it was good ... but not for long. Then came the dispatch table of subroutine references: and this was much better. But it became tedious to type the same stuff over and over again. Then came CGI::Application which handles this and so much more. It is not easy to get the hang of, however. For what it is worth, i didn't get it the first time i tried and i could code a dispatch table solution in my sleep! :) One last comment about one of your comments: "It seems as though all of the benefits of C::A are things that can be accomplished by simply adjusting your coding style; making more blocks of code into subroutines, etc." Do not mistake what you have just said with being against C::A. Instead, C::A force you to abstract more than the if-elsif-else brute force solution. But, as my uncle says ... brute force is still good. It's easier to understand how to write. But, i say it turns into spaghetti eventually.In the end, i think it's all about where you put stuff. Energy cannot be created or destroyed, and whereas code can be condensed and refactored, you still end up with code and i think the best decisions are the ones that make your code easier to maintain and easier to extend in the future. It's all about how quickly you want to solve the problem and how much the future matters to the project. jeffa L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat) | [reply] [d/l] |
by stevecomrie (Beadle) on Jan 13, 2004 at 17:33 UTC | |
In the end, i think it's all about where you put stuff. Energy cannot be created or destroyed, and whereas code can be condensed and refactored, you still end up with code and i think the best decisions are the ones that make your code easier to maintain and easier to extend in the future. It's all about how quickly you want to solve the problem and how much the future matters to the project.Well said, rarely on a project do you have more time then you need, and never should you be unconcerned about the future state of your code. | [reply] |
|
Re: Why CGI::Application?
by stevecomrie (Beadle) on Jan 13, 2004 at 16:30 UTC | |
If this is what you consider a big block of C::A setup stuff, then maybe you're right. But considering all I had to do to start building a new application was create my Application.pm file, inherit from my C::A Super Class module, and fill it with that bit code, I'd have to disagree with you. By inheriting from my own Super Class built on C::A, those 3 quick steps give me access to cgi query params, session management, login authorization, conf file access, template file access, database connection & querying, logic to define which users have access to which "if/else" or "run-mode" blocks, MIME headers, cookie access, error handling and numerous other features. What you're missing is that I can now concentrate on building the actual application logic at hand instead of worrying about where to store session information, connecting to the database, setting a cookie properly, loading an HTML::Template or T::T file, etc. By relying on a Super Class module you can build stand-alone application and large groups of applications that all use the same code base, thus you save yourself time, energy and headaches. Furthermore, by relying on compartmentalized code for basic operations you are safe to change those compartments at the Super Class level and know that all your applications will continue to run as they did before. Sure it's all something you could roll on your own or build as you go. Yes it's something that you could acomplish on your own by making your code better and more modular in the first place. All C::A is is a framework for building better applications faster. Maybe it's not the be-all-end-all of frameworks for build CGI app's but it's one of the best ones available right now, and it's supported by a large group of programmers like my self that try very hard to make sure it makes the easy things easy and the difficult things possible. I mean, after all .. we could all be programming in binary, why add another layer of complexity and confusion like PERL to the equation. | [reply] [d/l] |
|
Re: Why CGI::Application?
by Willard B. Trophy (Hermit) on Jan 13, 2004 at 15:01 UTC | |
With mod_perl and Apache::DBI (connection cacheing) I can get application response times so good I didn't think they were possible. The server load is way down too. If that were all, it would be enough, but you also get HTML::Template for free, and lots of handy prebuilt solution modules that might just do exactly what you want. -- | [reply] |
|
Re: Why CGI::Application?
by perrin (Chancellor) on Jan 13, 2004 at 16:31 UTC | |
| [reply] |
by rkg (Hermit) on Jan 13, 2004 at 21:46 UTC | |
Once upon a time I wrote my own wrapper for building CGI apps, then later discovered I was heading down the very same path as C:A (only not as well.) I'll use the space here to recommend Mason. It works very nicely with mod_perl and it is both powerful and easy. (And AMZN uses it on their core site, so that's a powerful endorsement.) An example: deep inside a web page, within Mason, I can detect the user wanted the report in Excel, vs. HTML. Cool. Clear the buffer, send the right content type header, pump out the Excel data, and then turn off Mason processing -- didn't have to worry if I had sent headers or page content already, runmodes, etc. And the Mason notion of components makes me feel like I am playing with Legos -- snap snap a new web page. Nope, move that function off this page to that page, sure. Snap, snap. Easy. Could just be the "honeymoon" infatuation with a new (for me) technology, but I'm currently a strong advocate of H:M. | [reply] [d/l] |
by dragonchild (Archbishop) on Jan 14, 2004 at 11:44 UTC | |
------
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified. | [reply] |
by DrHyde (Prior) on Jan 15, 2004 at 09:47 UTC | |
C::A may provide a way of avoiding spaghetti. So does thinking before touching the keyboard. A big if or switch or anything else does not automatically mean spaghetti. Spaghetti is a symptom of bad programming practices, and I don't care what modules you use, a bad programmer will always write nasty code. | [reply] [d/l] [select] |
by Anonymous Monk on Jan 14, 2004 at 12:13 UTC | |
| [reply] |
|
Re: Why CGI::Application?
by zby (Vicar) on Jan 13, 2004 at 14:09 UTC | |
The question that intrigues me is why there is another dispatch mechanism build using the run_modes method. Why the object dispatch is not enough? In what circumstances another redirection layer is needed? Update: run_modes not run_mode. Update: For security reasons there should be a list of methods that can be called from the web. That's unevitable, at least untill perl has public and private methods. | [reply] |
by derby (Abbot) on Jan 13, 2004 at 14:47 UTC | |
If I'm following your question correctly, the payoff of the run_modes method is so you can have multiple mappings of modes to methods. It's odd, but not unheard of, to have two differently named modes invoke the same method (think AUTOLOAD).
-derby | [reply] |
by zby (Vicar) on Jan 13, 2004 at 14:56 UTC | |
Is it so common place that the shorcut justifies the overhead of the additional run mode hash? | [reply] [d/l] |
|
Re: Why CGI::Application?
by weierophinney (Pilgrim) on May 07, 2004 at 00:29 UTC | |
Let's say I have a simple application that should do the following: How do I tie it all together? What CGI::Application does is allow me to focus on the issue at hand: how do I retrieve my content? how do I determine what content to retrieve? Now, I can focus on one aspect at a time - one method per action, and calling related methods when necessary. I leave my display issues to templates, and simply assign the data I pull to the templates. I have session handling kept in a single superclass, along with my database connections. All I have to do is get my content; the rest is taken care of. The CGI::App framework is setup in such a way that code reuse becomes not only easy, but the only way to effectively program. It enforces consistency, because you could be inheriting from any given module in order to extend it for further applications; because they all share the same framework, you don't have to worry about collisions in terminology when you mix and match. On top of that, it allows you to setup an easy inheritance framework such that you can have multiple application modules inheriting from the same parent so that the same look and feel are inherited; this means that all of your site's applications can easily share the same look and feel, as well as have a common underlying structure. And, if you want to, you can port your entire application to another site simply by changing the parameters you pass to it through the instatiation script -- new data stores, different group of templates, etc. Your reusability now extends not just to the current site, but to many sites. Sure, you can "adjust your coding style" so that code is more reusable; what CGI::Application does is give you a ready-made, rigorously tested, framework for reusable code. Personally, I've had enough of hand-made frameworks; I want something that not only I can understand, but many other people can understand and have used. | [reply] |
|
Re: Why CGI::Application?
by DrHyde (Prior) on Jan 13, 2004 at 13:24 UTC | |
And it's only big because each block consists of stitching together a big data structure by calling a load of object methods (and these are BIG structures, for complex pages) which is then passed to TT. There's not all that many statements in there. | [reply] [d/l] |
by derby (Abbot) on Jan 13, 2004 at 13:41 UTC | |
All it did was irritate me
How? Did it make your eyes water? Your scalp itch? A burning sensation in an area not discussed amongst mixed company? Look, I'm not much into promoting or even believing in any silver bullet theories but I must say CGI::Application has been a boon to my development. As another poster stated, it started me thinking about my webapps as state machines and that has been a good thing. Maybe your apps don't need that, if you have a simple CGI that does one thing only - then probably not. But once you get into a multi-step, multi-form, webapp, CGI::Application is definetly a plus. If you have the time or inclination, please write up what irritated you. Give us examples of how your approach is better ... are you sure it wasn't the small learing curve for CGI::Application that just got in the way of you delivering the product. -derby | [reply] |
by DrHyde (Prior) on Jan 14, 2004 at 10:55 UTC | |
It most certainly is a good thing! Maybe your apps don't need that, if you have a simple CGI that does one thing only - then probably not. But once you get into a multi-step, multi-form, webapp, CGI::Application is definetly a plus. CGI::Application makes it no easier to write multi-form applications as far as I can tell. All my form submissions have an action=foo parameter. The various foos have sensible names and simply make the big if ... elsif ... else block call the appropriately named subroutine. This is exactly the same as CGI::Application's "run modes" If you have the time or inclination, please write up what irritated you. It irritates me because it provides nothing useful, while wrapping itself up in grandiose language about how it will make the web application world a better place. Give us examples of how your approach is better ... I can't give you the code, but I consider it to be better because it has fewer dependencies while requiring me to do just as much work, and because it's one less thing for someone else to have to learn before maintaining my code. are you sure it wasn't the small learing curve for CGI::Application that just got in the way of you delivering the product. What learning curve? CGI::Application is far simpler than some of the other modules I'm using. I should write this up as a review for cpanratings. | [reply] [d/l] [select] |
by demerphq (Chancellor) on Jan 14, 2004 at 20:28 UTC | |
by DrHyde (Prior) on Jan 15, 2004 at 09:39 UTC | |
| |
|
Re: Why CGI::Application?
by Anonymous Monk on Jan 14, 2004 at 09:32 UTC | |
Don't take this as an invitation to stop giving feedback, though. :-P -Scott | [reply] |
by stevecomrie (Beadle) on Jan 14, 2004 at 14:45 UTC | |
If you have any questions or are having trouble working through your first app or two, would like to see some example application modules, or want some information on some C::A 'best practice' methedology, you can take a look at the following links: CGI::Application - Best Practices Wiki http://twiki.med.yale.edu/twiki2/bin/view/CGIapp/WebHome CGI::Application Mailing List Archive http://www.mail-archive.com/cgiapp@lists.erlbaum.net/ Or, send a blank e-mail cgiapp-subscribe@lists.erlbaum.net to subscribe to the mailing list, all questions and concerns will be addressed. | [reply] |
|
Re: Why CGI::Application?
by schweini (Friar) on Jan 18, 2004 at 02:02 UTC | |
but a lot of monks pointed out that the if/elsif constructs (that i use a lot) are inherently flawed, and i'd just like to take advantage and ask why? assuming one doesn't use any advanced features of C::A, does one actually gain a lot by using it? is calling a sub noticably faster than letting the perl interpreter run through all the conditions? why exactly does C::A increase code mainainabilty? to explain my situation: i bundle up anything related in seperate .pl files, which all do global start-up scripts (for session-handling, db-connections, etc.), and all 'interesting' and reusable subs are inside various modules that get used where appropiate. i'm just wondering whether i'd gain a lot by switching to C::A (which, AFAIK, wouldn't be tooooo hard), because of the tons of positive comments about it... | [reply] [d/l] [select] |
by dragonchild (Archbishop) on Jan 19, 2004 at 13:46 UTC | |
Now, there's a number of different architectures one can do. I'm going to assume you're using templates and an RDBMS (like MySQL or Oracle). I'm also going to assume you're using intelligent developer practices, such as design, documentation, and testing. I would consider that an evolutionary scale. C::A does a lot of the heavy lifting for you. It also is self-documenting. In other words, you don't have to think about (and potentially mess up) some of the nitty-gritty details. To me, that's a "Good Thing"(tm). ------
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified. | [reply] |