What are the pros and cons of using two different CGI.pm programming styles with Perl?

I participate in a web scripting class at school and am working on my first assignment. I tend to overdo things and go deeper into my subject than what is required in my classes. I am currently studying CGI.pm to fulfill my HTTP requests, and it says that there are two programming styles for CGI.pm:

  • Object oriented style
  • Function oriented style

If I have not missed a clear answer or are not sufficiently informed to find out the answer for myself from the documentation provided at: http://perldoc.perl.org/CGI.html I just don’t know what are the pros and cons of these two different styles.

With that said, what are the pros and cons of using two different styles? Which one is more commonly used? Regarding the use of an object oriented style, he says that I can only use one CGI object at a time. Why is this?

Thank you for your help. You all made learning computer science very enjoyable, satisfying, and useful to me. = D

+7
perl cgi
source share
2 answers

Behind the scenes, CGI.pm does the same, despite the styles. The functional interface actually uses a secret object that you do not see.

For many small CGI projects, you probably will never need more than one CGI object at a time, so the functional interface is fine. This may be a more common style, but only because most people make small scripts for very specific tasks. If you have many other things, you may not like the fact that CGI.pm imports a long list (and long) of function names into your script. Some function names may interfere with other modules that you want to import.

However, I always use an object-oriented interface. I don’t need to worry about name collisions, and this is obvious when a method comes from the moment you see its object. It is also easy to pass an object as arguments to other parts of large applications, etc.

Some people may complain about additional typing, but this has never been a slow part of programming for me. I have been doing Perl for a long time, and I am not against syntax. However, I use only CGI to input input and possibly send output. I do not interfere with any HTML material.

When he talks about one CGI.pm object at a time, he refers to access to the input. For example, if you read STDIN, another CGI.pm object will not be able to read this. However, you can have as many objects as you like. They simply will not share the data, and the first receives all the POST data.

In fact, you can use the mixture. You can import some things, for example :html , but still use the OO interface to process the input.

+17
source share

I highly recommend using the object interface.

Will it be absolutely necessary for your work in the classroom? No, actually this is probably too much for small projects.

However, if you are serious about learning to use CGI.pm for larger-scale projects, you will need to learn the object method. If you reach a point that requires two objects, you will have to use the object's interface. Programming, like everything else, improves with practice. Practicing relatively simple problems will help you be prepared for more complex ones.

In fact, I would recommend it as a general rule in programming (although there are exceptions) that if you come across two methods of using a specific tool that uses the habit of using the one that will most likely be used in production code and / or one that is The correct answer for most of the problem space.

+4
source share

All Articles