Permalink
Browse files

hopts POD

goto &readonly to improve error messaging
  • Loading branch information...
1 parent f3f4b7a commit e6b15cd1639083828fab5ec35e877fc95975c61c @jamhed jamhed committed Nov 23, 2010
Showing with 30 additions and 13 deletions.
  1. +21 −12 lib/selfvars.pm
  2. +5 −1 t/0hopts.t
  3. +4 −0 t/lib/HOpts.pm
View
@@ -91,16 +91,16 @@ sub readonly { require Carp; Carp::croak('Modification of a read-only @args atte
sub TIEARRAY { my $x; bless \$x => $_[0] }
sub FETCHSIZE { scalar $#{ _args() } }
-sub STORESIZE { readonly } # $#{ _args() } = $_[1] + 1;
+sub STORESIZE { goto &readonly } # $#{ _args() } = $_[1] + 1;
sub STORE { _args()->[ $_[1] + 1 ] = $_[2] }
sub FETCH { _args()->[ $_[1] + 1 ] }
-sub CLEAR { readonly } # $#{ _args() } = 0;
-sub POP { readonly } # my $o = _args(); (@$o > 1) ? pop(@$o) : undef
-sub PUSH { readonly } # my $o = _args(); push( @$o, @_ )
-sub SHIFT { readonly } # my $o = _args(); splice( @$o, 1, 1 )
-sub UNSHIFT { readonly } # my $o = _args(); unshift( @$o, @_ )
-sub DELETE { readonly } # my $o = _args(); delete $o->[ $_[1] + 1 ]
-sub SPLICE { readonly }
+sub CLEAR { goto &readonly } # $#{ _args() } = 0;
+sub POP { goto &readonly } # my $o = _args(); (@$o > 1) ? pop(@$o) : undef
+sub PUSH { goto &readonly } # my $o = _args(); push( @$o, @_ )
+sub SHIFT { goto &readonly } # my $o = _args(); splice( @$o, 1, 1 )
+sub UNSHIFT { goto &readonly } # my $o = _args(); unshift( @$o, @_ )
+sub DELETE { goto &readonly } # my $o = _args(); delete $o->[ $_[1] + 1 ]
+sub SPLICE { goto &readonly }
# my $ob = shift;
# my $sz = $ob->FETCHSIZE;
# my $off = @_ ? shift : 0;
@@ -164,12 +164,12 @@ sub readonly { require Carp; Carp::croak('Modification of a read-only %args atte
sub TIEHASH { my $x; bless \$x => $_[0] }
sub FETCH { my (%o) = _opts(); $o{ $_[1] } }
-sub STORE { readonly }
+sub STORE { goto &readonly }
sub FIRSTKEY { my (%o) = _opts(); my $a = scalar keys %o; each %o }
sub NEXTKEY { }
sub EXISTS { my (%o) = _opts(); exists $o{$_[1]} }
-sub DELETE { readonly }
-sub CLEAR { readonly }
+sub DELETE { goto &readonly }
+sub CLEAR { goto &readonly }
sub SCALAR { my (%o) = _opts(); scalar %o }
@@ -198,7 +198,7 @@ selfvars - Provide $self, @args and %opts variables for OO programs
use selfvars;
### Or name the variables explicitly:
- # use selfvars -self => 'self', -args => 'args', -opts => 'opts';
+ # use selfvars -self => 'self', -args => 'args', -opts => 'opts', -hopts => 'hopts';
### Write the constructor as usual:
sub new {
@@ -222,6 +222,11 @@ selfvars - Provide $self, @args and %opts variables for OO programs
$self->{x} = $opts{x};
$self->{y} = $opts{y};
}
+ ### Use %hopts with $obj->yada( x => 1, y => 2 ) call syntax
+ sub yada {
+ $self->{x} = $hopts{x}
+ $self->{y} = $hopts{y}
+ }
=head1 DESCRIPTION
@@ -264,6 +269,10 @@ Returns the argument list.
Returns the first argument, which must be a hash reference, as a hash.
+=item %hopts
+
+Returns the arguments list as a hash.
+
=back
=head2 Choosing non-default names
View
@@ -1,10 +1,14 @@
use lib 't/lib';
use HOpts;
-use Test::More tests => 1;
+use Test::More tests => 2;
{
my $o = HOpts->new;
is($o->hopts( param => 2 ), '0 2');
+ eval {
+ $o->die_in_action;
+ };
+ is($@, "Modification of a read-only %args attempted at t/lib/HOpts.pm line 16\n");
}
View
@@ -12,4 +12,8 @@ sub hopts {
return join ' ', $this->{v}, $args{'param'};
}
+sub die_in_action {
+ $args{'param'} = undef;
+}
+
1;

0 comments on commit e6b15cd

Please sign in to comment.