Skip to content

Commit

Permalink
Make URI::DefaultPort::default-port a method
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jonathanstowe committed Oct 23, 2016
1 parent 2e78b6c commit 1101ec0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.precomp
2 changes: 1 addition & 1 deletion 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" : {
Expand Down
2 changes: 1 addition & 1 deletion lib/URI.pm
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lib/URI/DefaultPort.pm
Expand Up @@ -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,
Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions 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;
}
}
18 changes: 18 additions & 0 deletions 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;

0 comments on commit 1101ec0

Please sign in to comment.