in reply to OO perl query
I believe it is generally more popular to use "question" where you used "doubt". That was easy to figure out. The second part of the question is a bit more confusing, but it sounds like you want to know how a method can be treated as a method or a regular subroutine.
If that is the case, parameter checking can help:
Now this is still not perfect but you know better than I what error conditions you need to handle. I also do want to mention that there should be no need for this. You can call one method inside another method without needing to treat it as a regular subroutine:sub generate_hash { my $self; if ( ref $_[0] && @_ == 4 ) { # We were called as a method $self = shift; } my ($i, $j, $k) = @_; }
As you can see, the method call works inside the method just fine. Typically the only reason you would want to change behavior based off parameters is if you wanted to provide modules with the option of non-OO functionality.sub new { my $class = shift; my $self = {}; bless $self, $class; $self->_initialize(@_); return $self; } sub _initialize { my $self = shift; # Do stuff with @_ }
Cheers - L~R
|
|---|