simple, practical example of "code-reuse with the help of OOP"

Discussion in 'Perl Misc' started by Rainer Weikusat, Apr 18, 2014.

  1. One of the problems "with OOP" is that realistic examples of its use are
    usually much too complicated to make good examples and that 'the usual
    contrived examples' (eg, drawing shapes or dealing with animals or cars)
    are too 'contrived' to be of much use. By chance, I came accross a
    'real' simple example.

    The problem I had to deal with was uploading a large file for processing
    to an appliance with 'very limited' disk space. Because of this,
    processing was supposed to work 'from the network', ie, without creating
    an intermediate temporary file. CGI.pm[*] offers an 'upload hook
    function' feature which can be used to handle the data as it arrives. An
    additional complication was that the values of some other fields of the
    same form needed to be known before the data could be processed. Clients
    are required to send form fields in the order they appear in the form
    so, theoretically, putting the other fields in front of the file upload
    control should work.

    [*] I spend more than half of my 'professional life' buried
    below heaps and heaps of variably grotty Java code and thus
    rejoice in every opportunity to use simple solutions to simple
    problems ...

    However, in practice, CGI.pm processes all input data during execution
    of the init-method which is invoked immediately before the constructor
    returns, meaning, there's no way the 'upload hook' function could use
    the CGI object to access the other parameters. Simple OOP-solution for
    that (code copyright my employer, cited for educational purposes):

    --------------
    package MAD::RestoreCGI;

    use CGI qw(-no_xhtml);
    our @ISA = qw(CGI);

    #* variables
    #
    our $delay_init;
    my %inits;

    #* methods
    #
    sub new_hull
    {
    local $delay_init = 1;
    return $_[0]->new(@_[1 .. $#_]);
    }

    sub init
    {
    my $args;

    if ($delay_init) {
    $inits{$_[0]} = [@_[1 .. $#_]];
    return;
    }

    $args = $inits{$_[0]};
    delete($inits{$_[0]});

    return $_[0]->SUPER::init(@$args);
    }

    # Ziegenzahn-Aschenbecher
    1;
    --------------

    The code using this invokes the new_hull ctor which returns the CGI
    object but prevents the actual init-method from being executed from the
    constructor itself. The init-method is then invoked explicitly to
    process the CGI input after the object has been made available to the
    upload-function which can then read the 'relating processing parameters'
    via $cgi->param('...') in the 'usual' way.

    NB: I don't claim that this is a particularly pretty or 'well-designed'
    solution, but it is simple and works.
     
    Rainer Weikusat, Apr 18, 2014
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    0
    Views:
    625
    Neil Zanella
    Nov 16, 2003
  2. Edwin Knoppert

    Sharing resources, practical? (webfarm)

    Edwin Knoppert, Feb 13, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    331
    Edwin Knoppert
    Feb 14, 2006
  3. sam

    Re: Practical .NET2 and C#2

    sam, Mar 17, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    437
    Patrick Smacchia
    Mar 17, 2006
  4. Jim
    Replies:
    3
    Views:
    902
    Will Hartung
    Sep 22, 2004
  5. Shawn Milo
    Replies:
    20
    Views:
    608
    Bruno Desthuilliers
    Mar 4, 2007
Loading...

Share This Page