Changeset 4953c44
- Timestamp:
- Feb 23, 2013, 5:44:23 PM (12 years ago)
- Branches:
- master, release-1.10
- Children:
- 6a8b519
- Parents:
- 4419d3d
- git-author:
- David Benjamin <davidben@mit.edu> (02/22/13 14:12:23)
- git-committer:
- David Benjamin <davidben@mit.edu> (02/23/13 17:44:23)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
perl/lib/BarnOwl.pm
re26abb6 r4953c44 437 437 get_tostring => sub { "$storage" }, 438 438 set_fromstring => sub { 439 return -1unless $_[0] =~ /^-?[0-9]+$/;439 die "Expected integer" unless $_[0] =~ /^-?[0-9]+$/; 440 440 $storage = 0 + $_[0]; 441 return 0;442 441 }, 443 442 validsettings => "<int>", … … 453 452 get_tostring => sub { $storage ? "on" : "off" }, 454 453 set_fromstring => sub { 455 return -1unless $_[0] eq "on" || $_[0] eq "off";454 die "Valid settings are on/off" unless $_[0] eq "on" || $_[0] eq "off"; 456 455 $storage = $_[0] eq "on"; 457 return 0;458 456 }, 459 457 validsettings => "on,off", … … 468 466 %{$args}, 469 467 get_tostring => sub { $storage }, 470 set_fromstring => sub { 471 $storage = $_[0]; 472 return 0; 473 }, 468 set_fromstring => sub { $storage = $_[0]; }, 474 469 validsettings => "<string>", 475 470 takes_on_off => 0, … … 492 487 get_tostring => sub { $storage }, 493 488 set_fromstring => sub { 494 return -1unless $valid{$_[0]};489 die "Invalid input" unless $valid{$_[0]}; 495 490 $storage = $_[0]; 496 return 0;497 491 }, 498 492 validsettings => join(",", @{$args->{validsettings}}) … … 514 508 The get/set functions are required. Note that the caller manages storage for the 515 509 variable. get_tostring/set_fromstring both convert AND store the value. 516 set_fromstring returns 0 on success.510 set_fromstring dies on failure. 517 511 518 512 If the variable takes parameters 'on' and 'off' (i.e. is boolean-looking), set … … 539 533 my $set_fromstring_fn = sub { 540 534 my ($dummy, $val) = @_; 541 $args{set_fromstring}->($val); 535 # Translate from user-supplied die-on-failure callback to expected 536 # non-zero on error. Less of a nuisance than interacting with ERRSV. 537 eval { $args{set_fromstring}->($val) }; 538 # TODO: Consider changing B::I::new_variable to expect string|NULL with 539 # string as the error message. That can then be translated to a GError in 540 # owl_variable_set_fromstring. For now the string is ignored. 541 return ($@ ? -1 : 0); 542 542 }; 543 543 -
t/variable.t
r4419d3d r4953c44 62 62 get_tostring => sub { "value is " . $value }, 63 63 set_fromstring => sub { 64 return -1unless $_[0] =~ /^...?$/;64 die "Too long" unless $_[0] =~ /^...?$/; 65 65 $value = $_[0]; 66 return 0;67 66 }, 68 67 takes_on_off => 1
Note: See TracChangeset
for help on using the changeset viewer.