Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

'new' constructors

by emcb (Beadle)
on Mar 16, 2002 at 03:27 UTC ( #152140=perlquestion: print w/replies, xml ) Need Help??

emcb has asked for the wisdom of the Perl Monks concerning the following question:


Im creating an application that that uses my own mods but i cant work out how the new func in say cgi works. I know the bless bits, but what i cant work out is how to pass vars to the new method like:

Frontier::Daemon->new( LocalPort => $listeningport, methods => $methods);

Any one know how i do this?


Replies are listed 'Best First'.
Re: 'new' constructors
by erikharrison (Deacon) on Mar 16, 2002 at 03:38 UTC

    Are you asking how to make constructors? If you are, the docs that come with Perl are pretty good in this regard. If you're just confused about passing arguments on to a constructor, this oughta help you:

    First, a method is the same as a subroutine. The only difference is it gets passed some extra info and is called a different way.. The @_ array contains what has been passed to the method - if the method is being called as a class method (like your constructor is) then the $_[0] will contain the class name, if it's an object method then it'll be the object itelf.

    A => is very similar to a comma - essentially it is used to pass arguments by name - this is what the CGI methods generally do (this includes the constructor). Both sides of the => are in the @_ in the order they were passed - but the best thing to do after getting the class or object shifted out of @_ is to assign the whole array to a hash, and use it that way.

    Hope this helsps

Re: 'new' constructors
by abstracts (Hermit) on Mar 16, 2002 at 03:32 UTC

    Here is how your package might look like:

    package Frontier::Daemon; sub new{ my $pkg = shift; # this would be Frontier::Daemon my %opts = @_; # these are the key/val pairs passed to you # $opts{LocalPort} # $opts{methods} # ... # and you can my $obj = \%opts; bless $obj, $pkg; return $obj; }
    Hope this helps,,,

    Aziz,,, </code>

Re: 'new' constructors
by jjohn (Beadle) on Mar 16, 2002 at 20:49 UTC


    I've worked a lot with Frontier::Daemon, so I'll try to answer your question. I confess that the way you phrased your question confuses me, so let me try to rephrase it.

    Frontier::Deamon is weird. I'm supposed to pass the constructor a hash reference whose keys are those of the XML-RPC Web Service API that I'm trying to implement and whose values are subroutine references. When XML-RPC clients talk to this bad boy, how are parameters passed to my subroutines?

    If this is your question, it's a good one. Frontier::Daemon is a subclass of HTTP::Daemon, which is another class whose new() method doesn't return. When a client connects to a Frontier::Daemon object, the request XML is parsed into a perl string. This string is then eval()'ed. The code that does this is in Frontier::RPC2::serve() and looks like this:

    eval { $result = &{ $methods->{$method} }(@{ $call->{'value'} })};

    Here, $method is the API name of the requested method and $call->{'value'} is an array reference that has the parameters to be passed to the "method." Method is an inaccurate term here: this isn't an object method call happening here at all. The $methods hash reference is the hash you passed in at Frontier::Daemon object instantiation. This may look a little weird if you're not up on references, but it's really using a code reference to invoke a subroutine. What's even stranger, perhaps, is that you could rewrite that line as:

    eval{ $methods->{$method}->( @{ $call->{values} } ) };

    This, to me, makes it look even more like a method call. Of course, it's not really.

    Now, this may not have been your question.

    Is your question more along the lines of:

    I've got an HTML form, some of whose values I want to pass to my Frontier::Daemon object. What kind of form handler do I need to do this?

    A common problem with the name "Web Service" is that it seems to imply CGI to folks. That's not right (apologies to Kevin Meanie). The "web" in Web Services refers to the fact that both web pages and "Web Services" travel across the wire speaking HTTP.

    To get HTML form values to be processed by your Web Service, you'll need to create a form handler in the language of your choice (Perl) which unpacks the CGI parameters, packs up the relevant values into an XML-RPC request, make the request, extract the response, the paint a new HTML page which does something with the answer. I wrote a few articles about XML-RPC and SOAP for IBM's developerWorks site (under 'web services'), so have a look there for copious details.

    Finally, if your question is:

    Man, Frontier::Daemon is really F'ed up! I want my module to screw with users' heads just like that! Where do I begin?

    To this, I can only say: Screw up your courage and dive into the source. For bonus points, do look at and There be dragons...

    Good luck.


      Thanks for taking the trouble eith that detailed reply but i was only using Frontier::Daemon as an example of the new method. Sorry, i didnt explain it a bit more.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://152140]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2023-06-09 11:40 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (35 votes). Check out past polls.