Can scrips include subroutines?

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?

What do you mean changes to the code? Like your scrip is changing the subroutine code?

Ah I understand, with some AI wording:

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:

  1. The first time it runs, sub _slack_api_call is defined

  2. On subsequent runs, Perl tries to define it again in the same namespace → “Subroutine redefined” warning

  3. 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);

You could also add:

 no warnings 'redefine';

To the code.

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.