Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Hmmm, I've been caught out by using accessors like that before. This is a more general code issue, and it's probably fine for your small code, but what happens if you want to store undef in your accessor?

    This is the reason I am leaning more and more towards Class::Accessor and similiar. I note that you'd have to override set() to return $self that way, however.

    -Dom
    • by wickline (135) on 2002.08.15 18:11 (#11818) Journal
      > what happens if you want to store undef

      I usually write something like
      sub foo {
          my $self = shift;
          return   @_  ? $self->{foo}=$_[0]  : $self->{foo};
      }
      which gives you the ability to set something to undef but doesn't let you chain method calls. If you wanted to chain method calls, you could instead write it this way
      sub foo {
          my $self = shift;
          if (@_) {
              $self->{foo} = shift;
              return $self;
          } else {
              return $self->{foo};
          }
      }
      -matt
      • Put these two subroutines somewhere

        sub non_chaining_accessor {
            my $name = (caller 1)[3];
            $name = substr $name, rindex($name,':')+1;
            $#_  ? $_[0]->{$name}=$_[1]  : $_[0]->{$name}
        }
        sub chaining_accessor {
            my $name = (caller 1)[3];
            $name = substr $name, rindex($name,':')+1;
            if ($#_) {
                $_[0]->{$name} = $_[1];
                return $_[0];
            } else {