in reply to Re^3: STDIN typeglob
in thread STDIN typeglob

sub new { my $class = shift; my %vars = @_; $vars{'error'} = ''; $vars{'reply'} = { 'status' => 'noaction', 'sent_to' => [ ], 'sent_to_all' => 'false', }; if (!exists{'payload'}) { # Obtaining payload from STDIN only # exists for backward compatability # This option is deprecated and will # be removed in a future version if (exists $ENV{'GATEWAY_INTERFACE'}) { read(STDIN, $vars{'payload'}, $ENV{'CONTENT_LENGTH'}); $vars{'webhook'} = decode_json($vars{'payload'}) if $vars{ +'payload'}; $vars{'error'} = 'No payload data' unless $vars{'webhook +'}; } else { $vars{'error'} = 'Looks like this is not a web request!' +; } } return bless \%vars, $class; }
It seems clearer and easier to test if this module were to simply accept a payload property

Is that a better constructor?
The production version of the module has been released so I don't want to break that. So the new method now accepts a payload property. If that is missing and it is running on a webserver with the right environment, it falls back to the previous behaviour.

A couple of versions down the line I will remove the fallback code and set an error condition if payload is not a valid JSON scalar.

Replies are listed 'Best First'.
Re^5: STDIN typeglob
by eyepopslikeamosquito (Archbishop) on Jun 14, 2023 at 00:24 UTC

    Is that a better constructor?

    Looks like a much better interface to me ... erm, after you fix the syntax error, by changing:

    if (!exists{'payload'}) {
    to:
    if (!exists $vars{'payload'}) {

    ;-) ... with the caveat that I have zero experience in the CGI domain.

    You should also add some new tests with dodgy payloads to verify they're being rejected with a useful error message.

      Looks like a much better interface to me...

      Well, it's now on its journey from PAUSE to CPAN...this and a few other enhancements have been included in Business::Stripe::Webhook v1.10