Changeset 4953c44


Ignore:
Timestamp:
Feb 23, 2013, 5:44:23 PM (12 years ago)
Author:
David Benjamin <davidben@mit.edu>
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)
Message:
Change set_fromstring to die on failure

Returning zero/non-zero is awkward. Leave a note for future improvement
to pass the error message along, although the default one is pretty
reasonable already.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • perl/lib/BarnOwl.pm

    re26abb6 r4953c44  
    437437            get_tostring => sub { "$storage" },
    438438            set_fromstring => sub {
    439                 return -1 unless $_[0] =~ /^-?[0-9]+$/;
     439                die "Expected integer" unless $_[0] =~ /^-?[0-9]+$/;
    440440                $storage = 0 + $_[0];
    441                 return 0;
    442441            },
    443442            validsettings => "<int>",
     
    453452            get_tostring => sub { $storage ? "on" : "off" },
    454453            set_fromstring => sub {
    455                 return -1 unless $_[0] eq "on" || $_[0] eq "off";
     454                die "Valid settings are on/off" unless $_[0] eq "on" || $_[0] eq "off";
    456455                $storage = $_[0] eq "on";
    457                 return 0;
    458456            },
    459457            validsettings => "on,off",
     
    468466            %{$args},
    469467            get_tostring => sub { $storage },
    470             set_fromstring => sub {
    471                 $storage = $_[0];
    472                 return 0;
    473             },
     468            set_fromstring => sub { $storage = $_[0]; },
    474469            validsettings => "<string>",
    475470            takes_on_off => 0,
     
    492487            get_tostring => sub { $storage },
    493488            set_fromstring => sub {
    494                 return -1 unless $valid{$_[0]};
     489                die "Invalid input" unless $valid{$_[0]};
    495490                $storage = $_[0];
    496                 return 0;
    497491            },
    498492            validsettings => join(",", @{$args->{validsettings}})
     
    514508The get/set functions are required. Note that the caller manages storage for the
    515509variable. get_tostring/set_fromstring both convert AND store the value.
    516 set_fromstring returns 0 on success.
     510set_fromstring dies on failure.
    517511
    518512If the variable takes parameters 'on' and 'off' (i.e. is boolean-looking), set
     
    539533    my $set_fromstring_fn = sub {
    540534      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);
    542542    };
    543543
  • t/variable.t

    r4419d3d r4953c44  
    6262    get_tostring => sub { "value is " . $value },
    6363    set_fromstring => sub {
    64         return -1 unless $_[0] =~ /^...?$/;
     64        die "Too long" unless $_[0] =~ /^...?$/;
    6565        $value = $_[0];
    66         return 0;
    6766    },
    6867    takes_on_off => 1
Note: See TracChangeset for help on using the changeset viewer.