From 1101ec09e7ea6c6e7ebfc0187aee50ceaa5f1992 Mon Sep 17 00:00:00 2001 From: Jonathan Stowe Date: Sun, 23 Oct 2016 19:27:51 +0100 Subject: [PATCH] Make URI::DefaultPort::default-port a method By making URI::DefaultPort a class and default-port a method it overcomes the problem when URI is used in a module which is subsequently 'require'd Fixes #30 --- .gitignore | 1 + META.info | 2 +- lib/URI.pm | 2 +- lib/URI/DefaultPort.pm | 6 +++--- t/lib/TestURIRequire.pm | 10 ++++++++++ t/require.t | 18 ++++++++++++++++++ 6 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100644 t/lib/TestURIRequire.pm create mode 100644 t/require.t diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ad9441d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.precomp diff --git a/META.info b/META.info index f642b82..9723fa6 100644 --- a/META.info +++ b/META.info @@ -1,6 +1,6 @@ { "name" : "URI", - "version" : "v0.1.1", + "version" : "v0.1.2", "description" : "A URI implementation using Perl 6 grammars to implement RFC 3986 BNF", "depends" : [ ], "provides" : { diff --git a/lib/URI.pm b/lib/URI.pm index 8194b4b..204cd0a 100644 --- a/lib/URI.pm +++ b/lib/URI.pm @@ -148,7 +148,7 @@ method host { } method default-port { - URI::DefaultPort::scheme-port($.scheme) + URI::DefaultPort.scheme-port($.scheme) } method default_port { $.default-port() } # artifact form diff --git a/lib/URI/DefaultPort.pm b/lib/URI/DefaultPort.pm index 10123be..9d0562c 100644 --- a/lib/URI/DefaultPort.pm +++ b/lib/URI/DefaultPort.pm @@ -3,7 +3,7 @@ use v6; # This logic seems to belong somewhere related to URI but not in the URI # module itself. -unit package URI::DefaultPort; +unit class URI::DefaultPort; my Int %default_port = ( ftp => 21, @@ -33,8 +33,8 @@ my Int %default_port = ( git => 9418 ); -our sub scheme-port(Str $scheme) { - return %default_port{$scheme}; +method scheme-port(Str $scheme) { + %default_port{$scheme}; } # vim:ft=perl6 diff --git a/t/lib/TestURIRequire.pm b/t/lib/TestURIRequire.pm new file mode 100644 index 0000000..b18c194 --- /dev/null +++ b/t/lib/TestURIRequire.pm @@ -0,0 +1,10 @@ +use v6.c; + +use URI; + +class TestURIRequire { + method test(Str $uri) { + my $u = URI.new($uri); + $u.port; + } +} diff --git a/t/require.t b/t/require.t new file mode 100644 index 0000000..e036eb5 --- /dev/null +++ b/t/require.t @@ -0,0 +1,18 @@ +#!/usr/bin/env perl6 + +use v6.c; + +use Test; +plan 2; + +use lib $*PROGRAM.parent.child('lib').Str; + +require TestURIRequire; + +my $port; + +lives-ok { $port = ::("TestURIRequire").test('http://example.com'); }, "can use URI in a module that is itself required rather than used"; +is $port, 80, "and got the right thing back"; + + +done-testing;