Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Difference between Function-oriented and object-oriented in Perl

by jesuashok (Curate)
on Nov 23, 2007 at 05:10 UTC ( #652498=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

dear monks,

# Function oriented use CGI qw(:standard); print header; print start_html("Hello World"); # Object Oriented use CGI; # don't need qw(:standard) $cgi = CGI->new; # ($cgi is now the object) print $cgi->header; # function call: $obj->function print $cgi->start_html("Hello World");
As you can see in the above 2 examples The CGI is used in function-oriented style and object-oriented style. But I coult not find any document which explains about the function oriented and object oriented way of using Perl modules. If any one can give me a brief explanation about that, would be a great help

Apologies if this has been discussed already



i m possible

Comment on Difference between Function-oriented and object-oriented in Perl
Download Code
Re: Difference between Function-oriented and object-oriented in Perl
by erroneousBollock (Curate) on Nov 23, 2007 at 05:37 UTC
    But I coult not find any document which explains about the function oriented and object oriented way of using Perl modules.
    I haven't really seen 'function-oriented' used as a term.

    Usually it's just called 'imperative' (as opposed to 'functional' which is something else altogether).

    Not everyone is going to agree with the classifications I provide below (different language backgrounds, etc), so I welcome improvements/clarifications to nomenclature.

    Not all Perl modules expose both kinds of interface, and where they do it's typically (not always) because the imperative interface is there for legacy users of the module.

    For imperative interfaces to modules, you get two main types:

    • "singleton": state/data held in module globals
    • "state-passing": you have to pass state/context to each call to a function.

    "singleton" imperative interfaces to modules offer the least level of composability with other APIs, and must generally have access to their functions serialised to avoid confusion or corruption. In your example above, CGI is first used via a "singleton" CGI interface; all information related to the request and the response are stored in module globals inside the CGI module.

    "state-passing" imperative interfaces are generally used by modules that don't have a lot of state. An example is Date::Calc; it exposes large number of functions that take simple scalars or lists and has no state data to speak of.

    OO interfaces for modules seem more popular these days, mostly I guess because they're a little more composable than imperative interfaces.

    Functional (lower-order FP) interfaces to modules are even less common, but can have even greater composability. File::Find and List::Util are probably good examples.

    -David

      Back in the days when objects were *just* being introduced to the world at large, the discussion usually revolved around procedural programming and object oriented programming. Given that jeshuashok is using function-oriented as opposed to procedural, it's just a matter of semantics. The potential confusion is that functional languages (e.g Lisp, Scheme, R) have yet another style..
        Yes, I tried to distinguish between 'imperative' and 'functional' programming.

        -David

Re: Difference between Function-oriented and object-oriented in Perl
by GrandFather (Cardinal) on Nov 23, 2007 at 06:25 UTC

    Object oriented interfaces to a module are useful where it is useful (or essential) to retain some state between calls into the module.

    Functional interfaces to modules are useful where a simple interface is desired and no state need be retained between calls into the module.

    Some modules provide both interface modes. List::Compare and List::Compare::Functional provide OO and Functional access to the same set of facilities. If you want to compare the same set of values in various ways the OO interface has a large advantage because a lot of preliminary work gets done only once. If you want to only compare the values in one way there is a slight advantage in terms of coding in using the functional interface.

    With Functional interfaces you generally have to option of polluting your name space with all the functions the module provides, or you have to remember to explicitly import the functions you require. With an OO interface you don't need to import any additional symbols into your name space at all.

    Generally if there is an OO interface available you are better to use it just because you avoid problems with importing symbols and potential ambiguities over how a particular symbol may be resolved.


    Perl is environmentally friendly - it saves trees
Re: Difference between Function-oriented and object-oriented in Perl
by Gavin (Canon) on Nov 23, 2007 at 13:29 UTC
    This, looks remarkably similar to your example.
    perhaps it will help to explain.
      Too much of a coincidence, not to be a copy and paste job.
      Be very aware jesuashok we are watching you!
Re: Difference between Function-oriented and object-oriented in Perl
by naikonta (Curate) on Nov 23, 2007 at 14:21 UTC
      Either you are misunderstanding what jeshuashok is asking, or else, I am.

      I don't think he's asking about the syntax of imperative programming, using functions, versus OO: after all, the example code he show, works. I think he's asking about why and when, one would choose for functions versus OO.

      And you list of tutorials doesn't even begin to address that question.

        bart, he is not asking anything. It is a shame that you front paged him.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://652498]
Approved by ikegami
Front-paged by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-09-22 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (208 votes), past polls