It's natural that you want to use uc like that.
First method - Call some methods on native types
You can use autobox for this in the following manner:
2 package MyArray;
3 use base qw(autobox);
4 sub uc { return map { uc } @{$_[0]}; };
5
6
7 package MyString;
8 use base qw(autobox);
9
10 sub split { [ split $_[1], $_[0] ] ; };
11
12
13 package main;
14
15
16 use autobox ARRAY => 'MyArray';
17 use autobox STRING => 'MyString';
18 use feature 'say';
19
20 say "one:two:three:four"->split(':')->uc();
or shorter:
1
2 use feature 'say';
3 use autobox ;
4
5 sub ARRAY::uc { return map { uc } @{$_[0]}; };
6 sub SCALAR::split { [ split $_[1], $_[0] ] ; };
7
8
9 say "one:two:three:four"->split(':')->uc();
Second method - Overriding the CORE method uc
This is simple , you just make up your own new method and you replace the one from CORE with your own in the BEGIN block
1
2
3 use feature 'say';
4
5 sub new_uc {
6 @_ > 1
7 ? map { uc } @_
8 : uc $_[0] ;
9 }
10
11 BEGIN {
12 *CORE::GLOBAL::uc = *new_uc;
13 }
14
15 say uc split(':',"one:two:three:four");
16
17 my $w = uc "a";
18 say $w;
By writing new_uc like that you assure that it will work as the old uc for scalars and in the new way for arrays
P.S. : There would be a third method .. to get into the Perl C sources in file pp.c at line 3620 and modify PP(pp_uc) so that it will behave as it should. Unfortunately that would require also reading av.c to understand how the arrays work , and reading or guessing what all of the macros do. Btw , has anyone else tried this before ? How in God's name do you start understanding those sources ?
|