PerlDancer / Dancer2

Perl Dancer Next Generation (rewrite of Perl Dancer)
http://perldancer.org/
Other
537 stars 272 forks source link

Exporting globals/code organization and import conflict #1708

Open amishHammer opened 3 months ago

amishHammer commented 3 months ago

As there is no longer a :syntax option for import there are no symbol conflicts for application's that export globals from sub modules. In this example we have a MyApp::Schemas module that contains a large number of OpenAPI shared schema objects (using Dancer2::Plugin::OpenAPI). These schema's are then re-used in other route handling modules.

App minimal layout lib/MyApp.pm lib/MyApp/V1/Object1.pm lib/MyApp/Schemas.pm

MyApp.pm

package MyApp;
use Dancer2 appname => 'MyApp';
use Dancer2::Plugin::OpenAPI;
use MyApp::V1::Object1;

1;

MyApp/V1/Object1.pm

package MyApp::V1::Object1;
use Dancer2 appname => 'MyApp';
use Dancer2::Plugin::OpenAPI;
use MyApp::Schemas;

prefix '/v1/object1';

openapi_path {
    tags => [ "Object1" ],
    description => "Get all of the objects currently registered",
    responses => {
        200 => {
            content => {
                "application/json" => {
                    schema => {
                        type => "object",
                        properties => { id => $IdSchema },
                    },
                },
            },
        },
    },
},
get '/' => sub {
    return { objects => { id => '0a244ad8-4ac6-4213-925c-dcb04d998803' } };
};
1;

MyApp/Schemas.pm

package MyApp::Schemas;
use Dancer2 appname => 'MyApp';
use Dancer2::Plugin::OpenAPI;

use Exporter qw/import/;
our @EXPORT = qw/$IdSchema/;

our $IdSchema = openapi_definition Id => {
    type => "string",
    description => "String containing the objects UUID",
};

When running the app you get the following error:

Subroutine MyApp::Schemas::import redefined at /usr/lib/x86_64-linux-gnu/perl-base/Exporter.pm line 30 (#1)