File: short-name-fallback

package info (click to toggle)
libsoftware-license-perl 0.103014-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,080 kB
  • sloc: perl: 7,270; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 2,234 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
Forwarded: https://github.com/Perl-Toolchain-Gang/Software-License/pull/44
Author: dod
Description: Short name fallback
 Patch short_name to fallback on a Software::License class if short name is unknown
--- a/lib/Software/LicenseUtils.pm
+++ b/lib/Software/LicenseUtils.pm
@@ -224,15 +224,21 @@
 
   Carp::croak "no license short name specified"
     unless defined $arg->{short_name};
-  my $short = delete $arg->{short_name};
-  Carp::croak "Unknow license with short name $short"
-    unless $short_name{$short};
+  my $subclass = my $short = delete $arg->{short_name};
+  $subclass =~ s/[\-.]/_/g;
 
-  my $info = $short_name{$short} ;
+  my $info = $short_name{$short} || "Software::License::$subclass";
   my @infos = ref $info ? @$info : ($info);
   my $lic_file = my $lic_class = shift @infos;
   $lic_file =~ s!::!/!g;
-  require "$lic_file.pm";
+  eval { require "$lic_file.pm"; } ;
+  Carp::croak "Unknow license with short name $short ($@)" if $@;
+
+  if (not $short_name{$short}) {
+      Carp::carp("Creating license from $short short name is deprecated. Please use ".
+                     "Software::LicenseMoreUtils");
+  }
+
   return $lic_class->new( { %$arg, @infos } );
 }
 
@@ -319,7 +325,7 @@
 
 Create a new L<Software::License> object from the license specified
 with C<short_name>. Known short license names are C<GPL-*>, C<LGPL-*> ,
-C<Artistic> and C<Artistic-*>
+C<Artistic> and C<Artistic-*>.
 
 =head2 new_from_spdx_expression
 
--- a/t/short_name.t
+++ b/t/short_name.t
@@ -2,7 +2,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 6;
+use Test::More tests => 8;
 
 my $class = 'Software::LicenseUtils';
 require_ok($class);
@@ -17,3 +17,16 @@
 isa_ok($license,'Software::License::GPL_1',"license class");
 like($license->name, qr/version 1/i, "license name");
 like($license->fulltext, qr/general public/i, 'license text');
+
+# test fall back
+my $mit_lic = $class->new_from_short_name({
+    short_name => 'MIT',
+    holder => 'X. Ample'
+});
+isa_ok($mit_lic,'Software::License::MIT',"license class");
+
+my $apache_lic = $class->new_from_short_name({
+    short_name => 'Apache-2.0',
+    holder => 'X. Ample'
+});
+isa_ok($apache_lic,'Software::License::Apache_2_0',"license class");