#!/usr/bin/perl use CGI qw(:standard); $age = ¶m('age'); print "Hi, you are $age years old!\n";¶m is a function that you give the name of an input field and it gets the value. One the command line, you can send that data this way:
perl age.pl age=21If this is sitting on a web server, you can put the parameters in the URL like this:
http://example.com/age.cgi?age=21It's common to use .cgi as the extension on CGIs on the web, but your server may let them work with .pl.
You want to create documentation for your API. This will list:
Jen's Age Stating API Description: Tells someone their age Endpoint: http://example.com/age.cgi Parameters: age (required) - value is age in years format - the format of the data. Options include XML or text. text is defaultYou can see I added in a format option there. That's something we should handle. It's common to return data in different formats. For this example, we would want to see what they sent (if they sent anything) and print out something different in each case. In the code below, note I use the function lc() which converts the argument to lower case.
#!/usr/bin/perl use CGI qw(:standard); $format="text"; $age = ¶m('age'); $format = lc(¶m('format')); if ($format eq "xml") { print "<age>$age</age>"; } else { print "Hi, you are $age years old!\n"; }
The subroutine can do anything you want, but you can have it send back a value. When you call the subroutine it gets replaced by the value you return.
#!/usr/bin/perl use CGI qw(:standard); $x = ¶m("number"); print square($x); sub square { $in = @_[0]; return $in*$in; }