I have a Scrip that benefits from the use of a subroutine. While the code works, it also generates a warning every time it runs, and changes to the subroutine code do not take affect. I’m even able to delete the subroutine then trigger the scrip and it will run as if the subroutine was still there.
In order to update that part of my code I must restart apache.
[warning]: Subroutine _slack_api_call redefined
Is this expected behavior? Should I turn my scrip into a plugin?
When RT loads your Scrip, it evaluates the code in the same Perl namespace/scope repeatedly without properly cleaning up. When you define a subroutine in a Scrip:
The first time it runs, sub _slack_api_call is defined
On subsequent runs, Perl tries to define it again in the same namespace → “Subroutine redefined” warning
The original subroutine remains in memory, so changes don’t take effect until Apache restarts
Solutions
Option 1: Use Anonymous Subroutines (Quick Fix)
Instead of naming your subroutine, use an anonymous subroutine assigned to a variable:
perl
# Before the subroutine definition, add:
our $slack_api_call;
$slack_api_call = sub {
my ($endpoint, $payload) = @_;
# ... your code here
};
# Then call it like:
$slack_api_call->($endpoint, $payload);
Or use a lexical variable with my (preferred if scope permits):
perl
my $slack_api_call = sub {
my ($endpoint, $payload) = @_;
# ... your code here
};
$slack_api_call->($endpoint, $payload);
Thanks for that reply. I used your lexical variable suggestion and was able to avoid those warnings and remove the need to bounce apache every time I modified my still-in-development scrip.