Example 1: Hello world!
The classic "Hello world" example, except that the hello()
function takes a name as an argument.
sub hello
{
my $name = shift;
return "Hello, $name!";
}
__END__
FUNCTION hello(name IN VARCHAR2) RETURN VARCHAR2
SQL> select hello('Jeff') as response from dual;
RESPONSE
-------------------------------------------------------
Hello, Jeff!
Example 2: MD5 checksum
This function uses the Digest::MD5 module from CPAN to return the MD5 checksum
of a string.
use Digest::MD5 qw(md5_hex);
sub md5
{
return md5_hex($_[0]);
}
__END__
FUNCTION md5(data IN VARCHAR2) RETURN VARCHAR2
SQL> select md5('foobar') from dual;
MD5('FOOBAR')
-------------------------------------------
3858f62230ac3c915f300c664312c63f
Example 3: Stock Quote
This retrieves a live stock quote from Yahoo. It accepts the stock symbol as
an argument. If you run this several times during normal trading hours on an
active stock, the value may change with each query, as the quote is retrieved
from the internet each time.
use Finance::Quote;
sub quote
{
my $sym = shift;
my $q = Finance::Quote->new();
my %h = $q->yahoo($sym);
return $h{$sym,'price'};
}
__END__
FUNCTION quote(symbol IN VARCHAR2) RETURN REAL
SQL> select quote('ORCL') from dual;
QUOTE('ORCL')
--------------------------------------------------------------------------------
12.29
SQL> /
QUOTE('ORCL')
--------------------------------------------------------------------------------
12.271