This file is indexed.

/usr/share/perl5/Rinci/function.pod is in librinci-perl 1.1.43-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
package Rinci::function; # just to make PodWeaver happy

# VERSION

1;
# ABSTRACT: Metadata for your functions/methods

__END__

=pod

=encoding UTF-8

=head1 NAME

Rinci::function - Metadata for your functions/methods

=head1 VERSION

version 1.1.43

=head1 SPECIFICATION VERSION

 1.1

=head1 INTRODUCTION

This document describes metadata for functions/methods. Since the metadata
properties describe features and the way a function works, this document also
describes how a function should support those properties.

This specification is part of L<Rinci>. Please do a read up on it first, if you
have not already done so.

=head1 SPECIFICATION

B<Result envelope>. Function should return an enveloped result to express error
code/message as well as actual result. The envelope can be produced by the
function itself, or added by a wrapper tool. Result envelope is modeled after
HTTP or L<PSGI> response; it is an array in the following format:

 [STATUS, MESSAGE, RESULT, META]

STATUS is a 3-digit integer, much like HTTP response status code and is
explained further in L</"Envelope status codes">. MESSAGE is a string containing
error message. RESULT (or PAYLOAD) is the actual content to be returned and can
be omitted or set to undef if the function does not need to return anything.
META is called result metadata, a defhash containing extra data, analogous to
HTTP response headers. Result metadata is specified further in L<Rinci::result>.

Some example of an enveloped results:

 [200, "OK", 42]
 [404, "Not found"]
 [500, "Can't delete foo: permission denied", {errno=>51}]
 [200, "Account created", {id=>9323},
  {undo_calls=>[["delete_account", {id=>9323}]]}]

As mentioned, an enveloped result can contain error code/message as well as the
actual result. It can also be easily converted to HTTP response message. And it
can also contain extra data, useful for things like the transaction protocol
(explained in L<Rinci::Transaction>).

B<Special arguments>. Special arguments are some known arguments that start with
dash (C<->) and serve special purposes. You need not specify them in the C<args>
metadata property. Examples of special arguments include C<-dry_run>,
C<-tx_action>, and they will be explained in other related sections/documents.

B<Functions vs methods>. Since in many programming languages (like Perl 5,
Python, Ruby, PHP) static functions are not that differentiated from methods,
functions and methods share the same Rinci spec. But there are certain
properties that can be used to declare if a function is (also) a method or not.
See C<is_func>, C<is_meth>, C<is_class_meth> properties below for details.

B<Multiple dispatch>. This specification also does not (yet) have any
recommendation on how to best handle functions in languages that support
multiple dispatch, like Perl 6: whether we should create multiple metadata or
just one. It is more up to the tool and what you want to do with the metadata.

=head2 Envelope status codes

In general, status codes map directly to HTTP response status codes. Below are
the suggestion on which codes to use (or avoid). An asterisk (C<*>) marks which
codes are not defined in HTTP specification and introduced by this
specification.

=over 4

=item * 1xx code

Currently not used.

=item * 2xx code - success

200 should be used to mean success.

206 can be used to signal partial content, for example: a C<read_file()>
function which accepts C<byte_start> and C<byte_end> arguments should return 206
when only partial file content is returned. But in general, use 200 as some
callers will simply check for this exact code (instead of checking for range
200-299).

=item * 3xx code - further actions needs to be taken by user agent (caller)

301 (moved) can be used to redirect callers to alternate location, although this
is very rare.

304 (not modified, nothing done). Used for example by setup functions to
indicate that nothing is being modified or no modifying action has been
performed (see Setup::* modules in CPAN).

331* (confirmation required). Function requires confirmation, for example if
action to be taken is dangerous or requires user's attention. Confirmation
message/prompt from function can be returned in the message, or in the
C<confirm_prompt> property (e.g. to provide translations). Confirmation from
caller is in the form of special argument C<-confirm> with boolean value of true
(TODO: A more detailed confirmation for different actions can be specified later
in the form of C<-confirm_XXX> special arguments.) For an example of application
of this, see L<Perinci::Tx::Manager>.

=item * 4xx code - client (caller) side error

400 (bad request, bad arguments) should be returned when the function encounters
invalid input. A function wrapper can return this code when the function
arguments fail the argument schema validation (specified in the C<args>
property).

401 (authentication required).

403 (forbidden, access denied, authorization failed).

404 (not found). Can be used for example by an object-retrieval functions (like
C<get_user()>) and the object is not found.

For object-listing functions (like C<list_users()>), when there are no users
found matching the requested criteria, 200 code should still be returned with an
empty result (like an empty array or hash).

Also in general, an object-deletion function (like C<delete_user()>) should also
return 200 (or perhaps 304, but 200 is preferred) instead of 404 when the object
specified to be deleted is not found, since the goal of the delete function is
reached anyway.

408 (request timeout).

409 (conflict). Can be used for example by a C<create_user()> function when
receiving an already existing username.

412 (precondition failed). Similar to 409, but can be used to indicate lack of
resources, like disk space or bandwidth. For lacking authentication and
authorization, use 401 and 403 respectively.

429 (too many requests).

(EXPERIMENTAL) 44x codes are reserved for function-specific codes. Each function
is free to define what each number means. However, this is not really encouraged
and should only be used if necessary. Function should perhaps stick to
predefined codes here. To return more detailed status, result metadata can be
used.

480* is general transaction error, e.g. transaction status is aborted so further
requests for this transaction is ignored until transaction is aborted.

484* (no such transaction).

=item * 5xx code - server (callee) side error

500 is the general code to use when a failure occurs during the execution of a
function. for example when a C<delete_file()> function fails to delete specified
file (though in this case it can also choose to return 403 instead, which is
more specific).

501 (not implemented)

503 (service unavailable). You can use this when service is temporarily
unavailable, e.g. when system load is too high, a required service is down, etc.
Users should try again at a later time.

507 (insufficient storage)

521 (maximum retries reached)

531* (bad metadata) is used when there is something wrong with the metadata.

532* (failure in recording transaction) when there is a failure in updating
transaction status or in preparing/committing/rolling back the transaction.

(EXPERIMENTAL) 54x codes are reserved for function-specific codes. Each function
is free to define what each number means. However, this is not really encouraged
and should only be used if necessary. Function should perhaps stick to
predefined codes here. To return more detailed status, result metadata can be
used.

Try not to use code greater than 555, as some tools use (CODE-300) for error
codes that must fit in one unsigned byte (like L<Perinci::CmdLine>).

=back

=head2 Property: is_func => BOOL

Specify that the function can be called as a static function (i.e. procedural,
not as a method). Default is true if unspecified, but becomes false if is_meth
or is_class_meth is set to true.

Example:

 # specify that function can be called a method *as well as* a static function
 is_meth => 1
 is_func => 1 # if not specified, will default to false after is_meth set to 1

=head2 Property: is_meth => BOOL

Specify that the function can be called as an instance (object) method. Default
is false.

Example:

 # specify that function is a method
 is_meth => 1

=head2 Property: is_class_meth => BOOL

Specify that the function can be called as a class method. Examples of class
methods include the constructor, but there are others. Default is false.

Example:

 # specify that function is a class method
 is_class_meth => 1

=head2 Property: args => HASH

Specify arguments. Property value is defhash of argument names and argument
specification. Argument name must only contain letters, numbers, and underscores
(and do not start with a number).

Argument specification is a hash containing these keys:

=over 4

=item * B<schema> => SCHEMA

L<Data::Sah> schema for argument value.

=item * B<default> => ANY

Give default value for argument. This takes precedence over schema, which can
also specify default value. This is useful if you want to share a common schema
over several arguments but want to have different default for each argument. For
example, you have a C<ticket_status> schema. In C<create_ticket> function you
want the default C<status> argument to be C<new>, while in C<reply_ticket> you
want the default C<status> to be C<answered>.

=item * B<summary> => STR

From DefHash. A one-line plaintext summary, much like the C<summary> property in
variable metadata.

=item * B<req> => BOOL

Specify that argument is required (although its value can be undef/null).
Default is false.

=item * B<description> => STR

From DefHash. A longer description of marked up text, much like the
C<description> property. It is suggested to format the text to 74 columns.

=item * B<tags> => ARRAY OF (STR|HASH)

From DefHash. An array of tags, can be used by tools to categorize arguments.
Not unlike the C<tags> property.

=item * B<pos> => INT

Argument position when specified in an ordered fashion, e.g. in an array. Starts
from zero.

=item * B<greedy> => BOOL

Only relevant if B<pos> is specified, specify whether argument should gobble up
all remaining values in an ordered argument list into an array.

=item * B<cmdline_aliases> => HASH

Specify aliases for use in command-line options (or other possibly suitable
situation where arguments are parsed from command-line-like options). Keys are
alias names, values are itself hashes (alias specification). Valid alias
specification keys: C<summary> (a string, optional), C<schema> (optional,
defaults to argument's schema), C<code> (a code to set argument value, optional,
will be given C<< (\%args, $val) >>); if not set, the default behavior is simply
to set the argument value).

=item * B<cmdline_on_getopt> => CODE

A hook that will be called when argument is specified as a command-line option.
In Perl, hook will be called with a hash argument containing this key: C<arg>
(str, argument name), C<value> (str, option value), C<args> (hash, the argument
hash defined so far).

This can be useful if you want to process a command-line option directly on a
per-option basis instead of getting the final resulting argument value. For
example (in Perl):

 args => {
     library => {
         schema            => ['array*' => of => 'str*'],
         cmdline_aliases   => { I => {} },
         cmdline_on_getopt => sub {
             my %args = @_;
             require lib;
             lib->import($args{value});
         },
     },
     module => {
         schema            => ['array*' => of => 'str*'],
         cmdline_aliases   => { M => {} },
         cmdline_on_getopt => sub {
             my %args = @_;
             require Module::Load;
             Module::Load::load($args{value});
         },
     },
 }

With command-line argument like this:

 -I dir1 -M mod1 -I dir2 -M mod2

Without any C<cmdline_on_getopt> hooks, the function will receive this argument
hash:

 { library => ['dir1', 'dir2'], module => ['mod1', 'mod2'] }

but there is no way to know the order of options being specified in the
command-line. With the hooks, the function can load modules correctly (e.g.
loading C<mod1> won't search in C<dir2> as that directory has not been added by
-I).

=item * B<cmdline_on_getarg> => CODE

Like C<cmdline_on_getopt> but will be called when argument is specified as a
command-line argument. For example:

 args => {
     foo => {
         schema            => ['array*' => of => 'str*'],
         pos               => 0,
         greedy            => 1,
         cmdline_on_getopt => sub { ... },
         cmdline_on_getarg => sub { ... },
     },
     bar => { ... },
 },

and the command-line argument:

 --foo o1 --bar o2 --foo o3 a1 a2

The C<cmdline_on_getopt> hook will be called twice for C<o1> and C<o3>, while
the C<cmdline_on_getarg> hook will be called twice with C<a1> and C<a2>.

=item * B<completion> => CODE

A code to supply argument value completion. Will be explained in the examples.

=item * B<element_completion> => CODE

A code to supply argument element value completion. Only applicable if argument
type is C<array>. Will be explained in the examples.

=item * B<cmdline_src> => STR

Specify how to get the value for this argument, when function is run as a
command-line program. Valid values include: C<file> (command-line argument value
will be treated as filename and function argument will be set to content of the
file), C<stdin> (means that program should get function argument from standard
input), or C<stdin_or_files> (means that program should get value from content
of files, or if none is specified, from standard input). Other sources might be
defined in the future.

If function argument's type is C<str> or C<array>, the whole standard input and
files will be slurped into memory. If function argument's type is C<stream> or
C<filehandle>, program should provide standard input and files as a filehandle
(like the diamond operator in Perl) so function can read input one record at a
time. Record is line, but specifying the record separator should perhaps be
possible in the future.

There should only be one argument with C<src> set to C<stdin> or
C<stdin_or_files>.

TODO: Define C<web_src> property and source for streaming web application.

TODO: A way to define record separator.

=back

Example function metadata and its implementation in Perl:

 $SPEC{multiply2} = {
     v => 1.1,
     summary => 'Multiple two numbers',
     args => {
         a => {
             summary => 'The first operand',
             description => '... a longer description ...',
             schema=>'float*',
             pos => 0,
             tags => ['category:operand'],
         },
         b => {
             summary => 'The second operand',
             description => '... a longer description ...',
             schema => 'float*',
             pos => 1,
             tags => ['category:operand'],
         },
         round => {
             summary => 'Whether to round result',
             description => '... a longer description ...',
             schema => [bool => {default=>0}],
             pos => 2,
             tags => ['category:options'],
             cmdline_aliases => {
                 r=>{},
                 R=>{summary=>'Equivalent to --round=0',
                     code=>sub {$_[0]{round}=0}},
             },
         },
     }
 };
 sub multiply2 {
     my %args = @_;
     my $res = $args{a} * $args{b};
     $res = int($res) if $round;
     [200, "OK", $res];
 }

By default, without any wrapper, the function is called with a named hash style:

 multiply2(a=>4, b=>3);  # 12

But with the information from the metadata, a wrapper tool like
Perinci::Sub::Wrapper is able to change the calling style to positional:

 multiply2(4, 3.1, 1);  # 12

A command-line tool will also enable the function to be called named options as
well as positional arguments:

 % multiply2 --a 2 --b 3
 % multiply2 2 --b 3
 % multiply2 2 3

As mentioned earlier, C<cmdline_alises> is parsed by command-line option parser:

 % multiply2 2 3.5 -r ; # equivalent to multiply2 2 3 --round
 % multiply2 2 3.5 -R ; # equivalent to multiply2 2 3 --noround (--round=0)

Aliases in C<cmdline_aliases> are not recognized as real arguments:

 multiply2(a=>4, b=>3, r=>0);  # unknown argument r

Another example (demonstrates C<cmdline_aliases>):

 $SPEC{smtpd} = {
     v => 1.1,
     summary => 'Control SMTP daemon',
     args    => {
         action => {
             schema => ['str*' => {in=>[qw/status start stop restart/]}],
             pos    => 0,
             req    => 1,
             cmdline_aliases => {
                 status => {
                     schema    => [bool=>{is=>1}],
                     summary   => 'Alias for setting action=status',
                     code      => sub { $_[0]{action} = 'status' },
                 },
                 start => {
                     schema    => [bool=>{is=>1}],
                     summary   => 'Alias for setting action=start',
                     code      => sub { $_[0]{action} = 'start' },
                 },
                 stop => {
                     schema    => [bool=>{is=>1}],
                     summary   => 'Alias for setting action=stop',
                     code      => sub { $_[0]{action} = 'stop' },
                 },
                 restart => {
                     schema    => [bool=>{is=>1}],
                     summary   => 'Alias for setting action=restart',
                     code      => sub { $_[0]{action} = 'restart' },
                 },
             },
         },
         force => {
             schema => 'bool',
         },
     },
 };

Another example (demonstrates B<greedy>):

 $SPEC{multiply_many} = {
     v => 1.1,
     summary => 'Multiple numbers',
     args    => {
         nums   => {
             schema => ['array*' => {of=>'num*', min_len=>1}],
             pos    => 0,
             greedy => 1
         },
     },
 };
 sub multiply_many {
     my %args = @_;
     my $nums = $args{nums};

     my $ans = 1;
     $ans *= $_ for @$nums;
     [200, "OK", $ans];
 }

After wrapping, in positional mode it can then be called:

 multiply_many(2, 3, 4);  # 24

which is the same as (in normal named-argument style):

 multiply_many(nums => [2, 3, 4]);  # 24

In command-line:

 % multiply-many 2 3 4

in addition to the normal:

 % multiply-many --nums '[2, 3, 4]'

B<completion>. This argument specification key specifies how to complete
argument value (e.g. in shell or L<Riap::HTTP>) and is supplied an anonymous
function as value. The function will be called with arguments: word=>... (which
is the formed word so far, ci=>0|1 (whether completion should be done
case-insensitively). The function should return an array containing a list of
possible candidates. For an example of implementation for this, see
L<Perinci::Sub::Complete> in Perl which provides tab completion for argument
values. Example:

 $SPEC{delete_user} = {
     v => 1.1,
     args => {
         username => {
             schema     => 'str*',
             pos        => 0,
             completion => sub {
                 my %args = @_;
                 my $word = $args{word} // "";

                 # find users beginning with $word
                 local $CWD = "/home";
                 return [grep {-d && $_ ~~ /^\Q$word/} <*>];
             },
         },
         force => {schema=>[bool => {default=>0}]},
     },
 };

When C<delete_user> is executed over the command line and the Tab key is
pressed:

 $ delete-user --force --username fo<tab>
 $ delete-user fo<tab>

then B<bash> will try to complete with usernames starting with C<fo>.

B<element_completion>. This is like B<completion>, but for array elements.
Argument type must be C<array>. Example:

 $SPEC{delete_users} = {
     v => 1.1,
     args => {
         usernames => {
             schema     => ['array*' => of => 'str*'],
             req        => 1,
             pos        => 0,
             greedy     => 1,
             element_completion => sub {
                 my %args = @_;
                 my $word = $args{word} // "";

                 # find users beginning with $word
                 local $CWD = "/home";
                 my $res = [grep {-d && $_ ~~ /^\Q$word/} <*>];

                 # exclude users already mentioned by user
                 my $ary = $args{args}{usernames};
                 $res = [grep {!($_ ~~ @$ary)}] @$res;

                 return $res;
             },
         },
     },
 };

When C<delete_users> is executed over the command line:

 $ delete-users c<tab> ; # will complete with all users beginning with c
 $ delete-users charlie c<tab> ; # will complete with users but exclude charlie
 $ delete-users charlie chucky <tab> ; # and so on

=head2 Property: args_as => STR

Specify in what form the function expects the arguments. The value is actually
implementation-specific since it describes the function implementation. For
example in L<Perinci> for Perl, these values are recognized: C<array>, C<hash>,
C<arrayref>, C<hashref>. This property is useful for wrapper to be able to
convert one form to another.

The default value is also left to the implementation.

For interimplementation communication (e.g. via L<Riap::HTTP> or L<Riap::TCP>),
named arguments are always used so this property is irrelevant.

=head2 Property: result => HASH

Specify function return value. It is a defhash containing keys:

=over 4

=item * B<summary>

From DefHash. Like the C<summary> property in variable metadata.

=item * B<description>

From DefHash. Like the C<description> property. Suggested to be formatted to 78
columns.

=item * B<schema> => SCHEMA

A Sah schema to validate the result (the third element in the envelope result).
This schema should only be tested if status is 200. See also: C<statuses>.

=item * B<statuses> => HASH

Can be used to specify different result schema for different statuses. For
example:

 statuses => {
     206 => {
         schema => 'str*',
     },
 }

=back

Note that since functions normally return enveloped result, instead of
returning:

 RESULT

your functions normally have to return an enveloped result:

 [STATUS, MESSAGE, RESULT, METADATA]

Examples:

 # result is an integer
 result => {schema => 'int*'}

 # result is an integer starting from zero
 result => {schema => ['int*' => {ge=>0}]}

 # result is an array of records
 result => {
     summary => 'Matching addressbook entries',
     schema => ['array*' => {
         summary => 'blah blah blah ...',
         of      => ['hash*' => {allowed_keys=>[qw/name age address/]} ]
     }]
 }

=head2 Property: result_naked => BOOL

If set to true, specify that function does not envelope its results. The default
is false, to encourage functions to create envelopes. However, wrapper should be
able to create or strip envelope if needed. For example, if you have
"traditional" functions which does not do envelopes, you can set this property
to true, and the wrapper can generate the envelope for the functions.

=head2 Property: examples => ARRAY

This property allows you to put examples in a detailed and structured way, as an
alternative to putting everything in C<description>.

Each example is a defhash, it specifies what arguments are used, what the
results are, and some description. It can be used when generating API/usage
documentation, as well as for testing data. It can also be used for testing
(function will be run with specified arguments and the result will be matched
against expected result). Known properties:

=over 4

=item * args => HASH

Arguments used to produce result. Can be converted to C<argv> by tool, e.g. when
displaying command-line eamples

=item * argv => ARRAY

An alternative to C<args>, for example when function is run from the
command-line. Can be converted to C<args> most of the time when wanting to
display examples in Perl instead of command-line.

=item * src => STR

An alternative to C<args> or C<argv>, to provide raw source code. See also:
C<src_plang>. This can be used to show more general examples. For example, you
can show how a function is used in an expression or code block, or how a
command-line program is used in a shell script.

Exactly one of C<args>, C<argv>, or C<src> must be specified.

=item * src_plang => STR

The programming language the examples source code C<src> is written in. Valid
values include: C<perl>, C<bash>.

Command-line interface tools will typically only show examples written in
C<bash> or other shells, while Perl module tools will typically only show
C<perl> examples.

Required if C<src> is specified.

=item * status => INT (default: 200)

Status from envelope. If unspecified, assumed to be 200.

=item * result => DATA

Expected result.

=item * summary => STR

From DefHash. A one-line summary of the example You should describe, in one
phrase or sentence, what the example tries to demonstrate. You can skip the
summary if the example is pretty basic or things are already clear from the
C<args> alone.

=item * description => STR

From DefHash. Longer marked up text about the example (e.g. discussion or things
to note), suggested to be formatted to 72 columns.

=item * tags => ARRAY

From DefHash.

=item * test => BOOL (default: 1)

Whether to actually test example or not. Examples are by default run as tests by
a test module (e.g. Perl module L<Test::Rinci>. Setting this to 0 disables this
example from being included in a test.

TODO: more detailed testing instruction (e.g. only test in release candidate,
or under certain environment flag, etc).

=back

Example:

 # part of metadata for Math::is_prime function
 examples => [
     {
         args => {num=>10},
         result => 0,
         # summary no needed here, already clear.
     },
     {
         args => {},
         result => 400,
         summary => 'Num argument is required',
     },

     {
         argv => [-5],
         result => 1,
         summary => 'Also works for negative integers',
     },
 ],

Another example demonstrating C<src> for a function called C<list_countries>:

 examples => [
     {
         src => 'for c in `list-countries`; do wget http://flags.org/country/$c; done',
         src_plang => 'bash',
     },
     {
         src => <<'EOT',
 my $res = list_countries(detail => 1, sort=>['-popsize']);
 die "Can't list countries: $res->[0] - $res->[1]" unless $res->[0] == 200;
 my $i = 0;
 for my $c (@{ $res->[2] }) { $i++; say "$i. $_->{name}'s population: $_->{popsize}";
 EOT
         src_plang => 'perl',
     },
 ],

=head2 Property: features => HASH

The C<features> property is a deffhash. It allows functions to express their
features. Each hash key contains feature name, which must only contain
letters/numbers/underscores.

Below is the list of defined features. New feature names may be defined by
extension.

=over 4

=item * feature: reverse => BOOL (default: 0)

If set to true, specifies that function supports reverse operation. To reverse,
caller can add special argument C<-reverse>. For example:

 $SPEC{triple} = {
     v => 1.1,
     args     => {num=>{schema=>'num*'}},
     features => {reverse=>1}
 };
 sub triple {
     my %args = @_;
     my $num  = $args{num};
     [200, "OK", $args{-reverse} ? $num/3 : $num*3];
 }

 triple(num=>12);              # => 36
 triple(num=>12, -reverse=>1); # =>  4

=item * feature: tx => HASH

Default is none. Specify transactional support, as specified in
L<Rinci::Transaction>. Value is a hash containing these keys: C<v> (int,
protocol version, default if not specified is 1).

Please see Rinci::Transaction for more details on transaction.

=item * feature: dry_run => BOOL (default: 0)

Default is false. If set to true, specifies that function supports dry-run
(simulation) mode. Example:

 use Log::Any '$log';

 $SPEC{rmre} = {
     summary  => 'Delete files in curdir matching a regex',
     args     => {re=>{schema=>'str*'}},
     features => {dry_run=>1}
 };
 sub rmre {
     my %args    = @_;
     my $re      = qr/$args{re}/;
     my $dry_run = $args{-dry_run};

     opendir my($dir), ".";
     while (my $f = readdir($dir)) {
         next unless $f =~ $re;
         $log->info("Deleting $f ...");
         next if $dry_run;
         unlink $f;
     }
     [200, "OK"];
 }

The above Perl function delete files, but if passed argument C<-dry_run> => 1
(simulation mode), will not actually delete files, only display what files match
the criteria and would have be deleted.

Specifying a function as supporting dry_run means, among others:

=over 4

=item * If dry_run is requested, function will have no side effects

It will behave like a pure function, and thus have the properties of a pure
function.

=back

=item * feature: pure => BOOL (default: 0)

If set to true, specifies that function is "pure" and has no "side effects"
(these are terms from functional programming / computer science). Having a side
effect means changing something, somewhere (e.g. setting the value of a global
variable, modifies its arguments, writing some data to disk, changing system
date/time, etc.) Specifying a function as pure means, among others:

=over 4

=item * it can safely be inculded in transaction without recording in journal;

=item * it can safely be included during dry run;

=back

=item * feature: immutable => BOOL

Default is false. If set to true, specifies that function always returns the
same result when given the same argument values. This enables optimization like
memoization. An example of an immutable function is C<sub { $_[0]+$_[1] }> where
its results only depend on the arguments. Example of a mutable function would be
C<rand()> or C<read()> that reads contents from a file.

=item * feature: idempotent => BOOL

Default is false. If set to true, specifies that function is idempotent.
Idempotency means that repeated invocation of a function (each with the same
arguments) will have the same effect as a single invocation. In other words,
extra invocation will not have any effect.

Some operations, like reading a database row or a file's content, is inherently
idempotent (or to be exact nullipotent). Another example is setting or updating
an entity to some specific value, or deleting some entity. Repeated invocation
of the operation will still sets the entity to the same value, or still deletes
the entity.

Some other operations are inherently non-idempotent, for example sending an
email. Repeated invocation will cause multiple emails to be sent.

Yet some other operations are non-idempotent, but can be made idempotent simply
by checking whether the target object(s) has (have) reached the final desired
state, (optionally additionally also checking whether they are in the correct
original state to begin with). For example, a function that renames a file can
record the original file that was renamed (its MD5 checksum, size, or what not)
or perhaps record the action in a history database or flag file, and refuse to
rename again if the file to be renamed is not the original file.

=back

=head2 Property: deps => HASH

This property specifies function's dependencies to various things. It is a hash
of dep types and values. Some dep types are special: C<all>, C<any>, and
C<none>.

 deps => {
     DEPTYPE => DEPVALUE,
     ...,
     all => [
         {DEPTYPE=>DEPVALUE, ...},
         ...,
     },
     any => [
         {DEPTYPE => DEPVALUE, ...},
         ...,
     ],
     none => [
         {DEPTYPE => DEPVALUE, ...},
         ....,
     ],
 }

A dependency can be of any type: another function, environment variables,
programs, OS software packages, etc. It is up to the dependency checker library
to make use of this information.

For the dependencies to be declared as satisfied, all of the clauses must be
satisfied.

Below is the list of defined dependency types. New dependency type may be
defined by an extension.

=over 4

=item * dep: env => STR

Require that an environment variable exists and is true, where true is in the
Perl sense (not an empty string or "0"; " " and "0.0" are both true). Example:

 env => 'HTTPS'

=item * dep: prog => STR

Require that a program exists. If STR doesn't contain path separator character
'/' it will be searched in PATH. Windows filesystem should also use Unix-style
path, e.g. "C:/Program Files/Foo/Bar.exe".

 prog => 'rsync'   # any rsync found on PATH
 prog => '/bin/su' # won't accept any other su

=item * dep: code => CODE

Require that anonymous function returns a true value after called, where the
notion of true depends on the host language. Example in Perl:

 code => sub {$>}  # i am not being run as root

Example in Ruby:

 "code" => Proc.new { Process.euid > 0 }  # i am not being run as root

=item * dep: tmp_dir => BOOL

If set to 1, specify that function requires temporary directory. Caller should
provide path to this using special argument C<-tmp_dir>.

=item * dep: trash_dir => BOOL

If set to 1, specify that function requires trash directory. Trash is not unlike
a temporary directory. Caller should provide path to trash directory using
special argument C<-trash_dir>.

Trash directory can be provided, e.g. by transaction manager (see
L<Rinci::Transaction>).

=item * dep: all => [DEPHASH, ...]

A "meta" type that allows several dependencies to be joined together in a
logical-AND fashion. All dependency hashes must be satisfied. For example, to
declare a dependency to several programs and an environment variable:

 all => [
     {prog => 'rsync'},
     {prog => 'tar'},
     {env  => 'FORCE'},
 ],

=item * dep: any => [DEPHASH, ...]

Like C<all>, but specify a logical-OR relationship. Any one of the dependencies
will suffice. For example, to specify requirement to alternative Perl modules:

 or => [
     {perl_module => 'HTTP::Daemon'},
     {perl_module => 'HTTP::Daemon::SSL'},
 ],

=item * dep: none => [DEPHASH, ...]

Specify that none of the dependencies must be satisfied for this type to be
satisfied. Example, to specify that the function not run under SUDO or by root:

 none => [
     {env  => 'SUDO_USER'   },
     {code => sub {$> != 0} },
 ],

Note that the above is not equivalent to below:

 none => [
     {env => 'SUDO_USER', code => sub {$> != 0} },
 ],

which means that if none or only one of 'env'/'code' is satisfied, the whole
dependency becomes a success (since it is negated by 'none'). Probably not what
you want.

=back

If you add a new language-specific dependency type, please prefix it with the
language code, e.g. C<perl_module>, C<perl_func>, C<ruby_gem>, C<python_egg>.
These dependency types have also been defined by some existing tools: C<deb>
(dependency to a Debian package), C<rpm> (dependency to an RPM package),
C<js_url> (loading a remote JavaScript script URL), C<file> (existence of a),
C<perl_run_func> (running a Perl subroutine and getting a successful enveloped
result). Some of these might be declared as part of the core dependency types in
the future.

=head1 FAQ

=head2 What is the difference between C<summary> or C<description> in the Sah schema and arg specification?

Example:

 {
     args => {
         src => {
             summary => "Source path",
             description => "...",
             schema => ["str*", {
                 summary => "...",
                 description => "...",
                 ...
             }],
             ...
         },
         dest => {
             summary => "Target path",
             description => "...",
             schema => ["str*", {
                 summary => "...",
                 description => "...",
                 ...
             }],
             ...
         },
         ...
     },
 }

As you can see, each argument has a C<summary> and C<description>, but the
schema for each argument also has a C<summary> and C<description> schema
clauses. What is the difference and which should be put into which?

The argument specification's C<summary> (and C<description>) describe the
argument itself, in this example it says that C<src> means "The source path" and
C<dest> means "The target path". The argument schema's C<summary> (and
C<description>) describe the data type and valid values. In this example it
could say, e.g., "a Unix-path string with a maximum length of 255 characters".
In fact, C<src> and C<dest> are probably of the same type ("Unix path") and can
share schema.

 {
     ...
     args => {
         src => {
             ...
             schema => "unix_path",
         },
         dest => {
             ...
             schema => "unix_path",
         },
         ...
     },
 }

=head2 What is the difference between setting req=>1 in the argument specification and req=>1 in schema?

Example:

 # Note: remember that in Sah, str* is equivalent to [str => {req=>1}]
 args => {
     a => {         schema=>"str"  },
     b => {         schema=>"str*" },
     c => { req=>1, schema=>"str"  },
     d => { req=>1, schema=>"str*" },
 }

In particular look at C<b> and C<c>. C<b> is not a required argument (no req=>1
in the argument spec) but if it is specified, than it cannot be undef/null
(since the schema says [str=>{req=>1}], a.k.a "str*"). On the other hand, C<c>
is a required argument (req=>1 in the argument spec) but you can specify
undef/null as the value. The following are valid:

 func(c=>undef, d=>1);

But the following are not:

 func(b=>1, d=>1);  # c is not specified
 func(b=>undef, c=>1, d=>1);  # b has undef value
 func(b=>1, c=>1, d=>undef);  # d has undef value

=head2 Should I add a new metadata property, or add a new feature name to the C<features> property, or add a new dependency type to the C<deps> property?

If your property describes a dependency to something, it should definitely be a
new dependency type. If your property only describes what the function can do
and does not include any wrapper code, then it probably goes into C<features>.
Otherwise, it should probably become a new metadata property.

For example, if you want to declare that your function can only be run under a
certain moon phase (e.g. full moon), it should definitely go as a new dependency
type, so it becomes: deps => { moon_phase => 'full' }.

Another example, C<reverse> is a feature name, because it just states that if we
pass C<-reverse> => 1 special argument to a reversible function, it can do a
reverse operation. It doesn't include any wrapper code, all functionality is
realized by the function itself. On the other hand, C<timeout> is a metadata
property because it involves adding adding some wrapping code (a timeout
mechanism, e.g. an eval() block and alarm() in Perl).

=head1 SEE ALSO

Related specifications: L<Sah>, HTTP/1.1 (RFC 2068)

L<Rinci>

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/Rinci>.

=head1 SOURCE

Source repository is at L<https://github.com/sharyanto/perl-Rinci>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Rinci>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut