Changeset 69f74c2 for perl


Ignore:
Timestamp:
Feb 19, 2013, 8:29:34 PM (8 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master
Children:
3b9ca71
Parents:
e5210c9
git-author:
David Benjamin <davidben@mit.edu> (06/09/12 23:05:48)
git-committer:
David Benjamin <davidben@mit.edu> (02/19/13 20:29:34)
Message:
And now... the moment you've all been waiting for...

Expose OWL_VARIABLE_OTHER variables to perl. Rewrite all perl variable
entry points to use that one. From now on, C only thinks of these
variables as strings.

(This has one regression to be fixed in a later commit: you can't
set/unset with no arguments since we don't know how to set from
boolean.)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/lib/BarnOwl.pm

    r374089a r69f74c2  
    426426
    427427sub new_variable_int {
    428     unshift @_, \&BarnOwl::Internal::new_variable_int, 0;
     428    unshift @_, 0, "<int>", sub { "$_[0]" }, # to string
     429                            sub { $_[0] =~ /^-?[0-9]+$/ }, # validate
     430                            sub { 0 + $_[0] }; # from string
    429431    goto \&_new_variable;
    430432}
    431433
    432434sub new_variable_bool {
    433     unshift @_, \&BarnOwl::Internal::new_variable_bool, 0;
     435    unshift @_, 0, "on,off", sub { $_[0] ? "on" : "off" }, # to string
     436                             sub { $_[0] eq "on" || $_[0] eq "off" }, # validate
     437                             sub { $_[0] eq "on" }; # from string
    434438    goto \&_new_variable;
    435439}
    436440
    437441sub new_variable_string {
    438     unshift @_, \&BarnOwl::Internal::new_variable_string, "";
     442    unshift @_, "", "<string>", sub { $_[0] }, # to string
     443                                sub { 1 }, # validate
     444                                sub { $_[0] }; # from string
    439445    goto \&_new_variable;
    440446}
    441447
    442448sub _new_variable {
    443     my $func = shift;
    444449    my $default_default = shift;
     450    my $validsettings = shift;
     451    my $tostring_fn = shift;
     452    my $validate_fn = shift;
     453    my $fromstring_fn = shift;
     454
    445455    my $name = shift;
    446456    my $args = shift || {};
     
    450460        default     => $default_default,
    451461        %{$args});
    452     $func->($name, $args{default}, $args{summary}, $args{description});
     462
     463    # Store the value in a closure.
     464    my $value = $args{default};
     465
     466    my $get_tostring_fn = sub { $tostring_fn->($value) };
     467    my $set_fromstring_fn = sub {
     468      my ($dummy, $newval) = @_;
     469      return -1 unless $validate_fn->($newval);
     470      $value = $fromstring_fn->($newval);
     471      return 0;
     472    };
     473
     474    BarnOwl::Internal::new_variable($name, $args{summary}, $args{description}, $validsettings,
     475                                    $get_tostring_fn, $set_fromstring_fn, undef);
    453476}
    454477
Note: See TracChangeset for help on using the changeset viewer.