sub phoneFormat{
my @sigils = ('+','/','-'); # joiners
$_ = reverse(shift); # input; reversed for matches
@_ = grep{defined} unpack "A4A3A3A1",$_; # match, starting with extension; and remove unmatched
$_ = join('', map {$_ . pop @sigils } @_ ); # add in the delimiters
($_ = reverse) =~ s/^[^\d]+//; # reverse back and remove leading non-digits
$_;
}
print phoneFormat('012') , "\n"; # (blank)
print phoneFormat('0123') , "\n"; # 0123
print phoneFormat('0123456') , "\n"; # 012-3456
print phoneFormat('0123456789') , "\n"; # 012/345-6789
print phoneFormat('01234567899') , "\n"; # 0+123/456-7899
print phoneFormat('012345678999') , "\n"; # 1+234/567-8999