Skip to content

Instantly share code, notes, and snippets.

@cPanelRyan
Last active September 11, 2020 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cPanelRyan/c18cbb09ba8a2f1f4e39f363063f80b5 to your computer and use it in GitHub Desktop.
Save cPanelRyan/c18cbb09ba8a2f1f4e39f363063f80b5 to your computer and use it in GitHub Desktop.
Hook Action Code: Perl Script Example
#!/usr/local/cpanel/3rdparty/bin/perl
# Return errors if Perl experiences problems.
use strict;
use warnings;
# Use objects to handle input.
use IO::Select;
# Properly decode JSON.
use JSON::Syck;
# Get decoded input.
my $input = get_passed_data();
# Declare return variables and set their values.
my ( $result_result, $result_message ) = do_something($input);
# Return the return variables and exit.
print "$result_result $result_message";
exit;
# Perform the hook's action.
sub do_something {
# Get the input data.
my ($input) = @_;
# Set success and failure messages.
my $result = 1; # This Boolean value is set to fail.
my $message = 'This is an error message.'; # This string is a reason for $result.
# Insert your actions here.
# Return the hook result and message.
return $result, $message;
}
# Process data from STDIN.
sub get_passed_data {
# Declare input variables.
my $raw_data = '';
my $input_data = {};
# Set up an input object.
my $selects = IO::Select->new();
# Get input from STDIN.
$selects->add( \*STDIN );
# Process the raw output, and JSON-decode.
if ( $selects->can_read(.1) ) {
while (<STDIN>) {
$raw_data .= $_;
}
$input_data = JSON::Syck::Load($raw_data);
}
# Return the output.
return $input_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment