cpousset has asked for the wisdom of the Perl Monks concerning the following question:
Any feedback or other comments?
The code below works properly.
#!/usr/bin/perl -w use strict; package xpkg; sub new { my $pkg = shift; # What class are we constructing? my %info = @_; # Allocate new memory bless(\%info, $classname); # Mark it of the right type return \%info; # And give it back } sub mproc { my ($ps) = shift; # either pkg or self my ($pkg,$info); # $info is a ref to hash if (ref $ps eq '') { # it is a function call $pkg = $ps; $info = shift; } else { # it is a method call $pkg = ref $ps; $info = $ps; } $info->{x} = 'y'; print $pkg; } my $x1 = xpkg->new(a => 1); $x1->mproc(); # call as method xpkg->mproc({b => 2}); # call as function
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: feedback on subroutine as both function and method in one
by davorg (Chancellor) on Jul 12, 2001 at 18:07 UTC | |
|
Re: feedback on subroutine as both function and method in one
by jeroenes (Priest) on Jul 12, 2001 at 18:53 UTC | |
|
Re: feedback on subroutine as both function and method in one
by bikeNomad (Priest) on Jul 12, 2001 at 20:45 UTC | |
|
Re: feedback on subroutine as both function and method in one
by andreychek (Parson) on Jul 12, 2001 at 21:28 UTC | |
by bikeNomad (Priest) on Jul 12, 2001 at 22:58 UTC |