|

Perl Programming Examples: Email, Encryption, Zip, HTTP, XML, FTP
Chilkat Software Components
Perl function syntax, Perl function declaration and arguments syntax.
Back
Question:
How do I write a function in Perl?
Answer:
An example of a Perl function is provided below. This one is called with 4 arguments: $crypt, $key, $clearBytes, and $expectedAnswer.
The point of this example is not to understand what this particular Perl function accomplishes, but rather to see a real-world example of the Perl function syntax.
sub blowfishECB {
my($crypt, $key, $clearBytes, $expectedAnswer) = @_;
# Set the secret key
$crypt->SetEncodedKey($key, "hex");
# Get the unencrypted data as binary data.
$unencryptedData = new chilkat::CkByteData();
$crypt->Decode($clearBytes, "hex",$unencryptedData);
$encryptedHexString = new chilkat::CkString();
$crypt->EncryptBytesENC($unencryptedData,$encryptedHexString);
# Chilkat padded to a multiple of 16 bytes, so we discard the padding.
# (In this case, Chilkat added 8 NULL bytes to the original 8 bytes of data,
# so we drop 16 characters - the hex encoding uses 2 characters per byte.)
$encryptedHexString->shorten(16);
# Return a string that will be added to our output for visual verification.
return $encryptedHexString->getString() . " should equal " . $expectedAnswer;
}
|