You should have some object $x (you usually get one with an object oriented interface), and you would do: $x->server('local.website');
A package is just a separate name space for variables and functions. A module is a package crafted so that it can be used as a reusalbe library
| [reply] [d/l] |
This code should belong to a package/class (let's call it Request::Server for example).
You can override server/port/etc. settings right after you've created your instance with:
my $server = Request::Server->new;
# code for set_defaults executed...
# and you override the default before using your object
$server->server('qa.website.net');
# and do the rest...
$server->start; # just an example!
| [reply] [d/l] |
| [reply] |
This is because in the environment where you are calling the server(), $self doesn't have the object in it. $self has the object in the Business::OnlinePayment class because it follows the Perl standard for a method:
sub server {
my $self = shift;
...
}
A Perl method call automatically adds the object that the method is being called on to the beginning of the parameter list. This allows the method to address things in the object (for instance, the server name).
I suspect two things are happening in your code:
- You didn't use strict, or you just added a my $self; without knowing why you were doing it.
- You didn't instantiate a Business:OnlinePayment object, or if you did, you didn't call set_defaults with it, as opposed to on it (a regular subroutine call instead of a method call).
Remember: you aren't in the Business::OnlinePayment namespace wen you create set_defaults (although you could be, see below), so you need to call it like this:
my $payer = Business::OnlinePayment->new();
set_defaults($payer);
Your code could add the set_defaults method to the class dynamically by defining it as
sub Business::OnlinePayment::set_defaults {
... your code here ...
}
If you instantiate the Business::OnlinePayment class with this subroutine defined, then you'll be able to say
my $payer = Business::OnlinePayment->new();
$payer->set_defaults();
as defining the sub with a fully-qualified name automatically adds it to the namespace, and if it's in the namespace, Perl will treat it as if it were a method defined in the Business/OnlinePayment.pm file, no matter where it was actually defined. I would recommend you not do this unless you comment it extensively! The next person along will not be expecting this, and if you invoke $payer->set_defaults() he or she will expect to find that in Business/OnlinePayment.pm and will confused and irritated if it is not there but works. | [reply] [d/l] [select] |