in reply to Trouble getting started with Perl OO

If you want to use named parameters, I suggest you use a named parameter parser. I have attached my version of the named (switched ) paramter parser as well as my version of the shapes module.
package NP; require 5.00503; require Exporter; use strict; use warnings; use Carp; use Data::Dumper; use vars qw/ @ISA @EXPORT /; @ISA = qw/ Exporter /; @EXPORT = qw/ ParseNamedParameters /; sub ParseNamedParameters { my $allowed_parameters = shift; my @param = split /\|/, $allowed_parameters; my %param = (); return undef unless scalar @_; # must have something to decode fir +st if (ref $_[0] eq 'HASH') { # Parameters passed in as HASHREF for my $p (keys %{$_[1]}) { if ($p !~ /^(?:$allowed_parameters)$/i) { print "Unknown parameter '$p', must be [$allowed_param +eters]\n"; } else { $param{'_' . lc $p} = $_[1]->{$p}; } } } elsif ($_[0] =~ /^-(?=$allowed_parameters)/i) { # Parameters passed in as named parameters my (%p) = @_; for my $p (keys %p) { if ($p !~ /^-(?:$allowed_parameters)$/i) { print "Unknown parameter '$p', must be [$allowed_param +eters]\n" } else { $param{'_' . lc substr($p,1)} = $p{$p}; } } } else { for my $p (@param) { my $val = shift; $param{'_' . lc $p} = $val; } } return \%param; } 1; package shapes; use NP; sub new { my $class = shift || "shapes"; my $self = {}; bless $self, $class; my $allowed_parameters = "shape"; # only decode parameters for the base object # derived objects have to decode their own parameters if ($class eq "shapes") { my $param = ParseNamedParameters($allowed_parameters, @_); @{$self}{keys %$param} = values %$param; } return $self; } sub print_shape { my $self = shift; print $self->{_shape}{line1} . "\n"; print $self->{_shape}{line2} . "\n"; print $self->{_shape}{line3} . "\n"; } 1; #!/usr/bin/perl -w use strict; use lib "./"; use shapes; my %box; $box{line1} = ' ---'; $box{line2} = '| |'; $box{line3} = ' ---'; my $box = shapes->new(-shape => \%box); $box->print_shape();

Replies are listed 'Best First'.
Re^2: Trouble getting started with Perl OO (ref $f eq 'HASH' is bad)
by demerphq (Chancellor) on Oct 07, 2004 at 13:23 UTC

    if (ref $_[0] eq 'HASH') {

    This seems to a bit of a religious subject, but personally I consider such checks as this to be totally wrong. You probably really want one of the below:

    #1 if (eval{ $_[0]->{''}, 1 }) { #2 use Scalar::Util qw(reftype); if (reftype $_[0] eq 'HASH') { #3 if (UNIVERSAL::isa($_[0],'HASH')) {

    I only consider the first to be really robust, the second is fairly robust but could do the wrong thing if the object was actually a different type but had hash overloading semantics. The third is passable, but is easily fooled into causing errors, and the version you used suffers from all of the problems mentioned for my examples as well as the flaw that it plain and simply can't be used with blessed hashes which IMO is just bad design.

    Anyway, you may not agree with my POV here, but if you haven't thought your position through on this type of thing its probably worth doing so.

    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi

      Flux8


Re^2: Trouble getting started with Perl OO
by Anonymous Monk on Oct 07, 2004 at 12:43 UTC
    If I want to use named arguments, most of the time I just assign @_ to a hash (which works for the majority of the cases, where all you can pass are key value pairs). If I want to do any "parsing", I use Getopt::Long. Why reinvent the wheel?
      Well, it's doing more than key value pairs, it also allow traditional parameter passing methods.

      mysub( $v1, $v2, $v3, $v4 ); mysyb( -v1 => $v1, -v3 => $v3 );

      I like to have the freedom of using either calling convension. Just a personal preference.