- my ($self) = @_;
- my (undef, $auth) = uri_split($$self);
- $auth =~ s/@.*\z// or return undef; # drop host:port
- $auth =~ s/;.*\z//; # drop ;AUTH=...
- $auth =~ s/:.*\z//; # drop password
- uri_unescape($auth);
+ my ($self, $val) = @_;
+ my ($scheme, $auth, $path, $query) = uri_split($$self);
+ my $at_host_port;
+ $auth =~ s/(@.*)\z// and $at_host_port = $1; # stash host:port for now
+ if (scalar(@_) == 2) { # set, this clobbers password, too
+ if (defined $val) {
+ my $uval = uri_escape($val);
+ if (defined($at_host_port)) {
+ $auth =~ s!\A.*?(;AUTH=$achar).*!$uval$1!ix
+ or $auth = $uval;
+ } else {
+ substr($auth, 0, 0) = "$uval@";
+ }
+ } elsif (defined($at_host_port)) { # clobber
+ $auth =~ s!\A.*?(;AUTH=$achar).*!$1!i or $auth = '';
+ if ($at_host_port && $auth eq '') {
+ $at_host_port =~ s/\A\@//;
+ }
+ }
+ $at_host_port //= '';
+ $$self = uri_join($scheme, $auth.$at_host_port, $path, $query);
+ $val;
+ } else { # read-only
+ $at_host_port // return undef; # explicit undef for scalar
+ $auth =~ s/;.*\z//; # drop ;AUTH=...
+ $auth =~ s/:.*\z//; # drop password
+ $auth eq '' ? undef : uri_unescape($auth);
+ }