Experts -
I found a script on one of the servers that I work on and I need help understanding
one of the lines.
I know what the script does, but I'm having a hard time understanding the grouping.
Can someone help me with this?
Here's the script...
#!/usr/bin/perl
use strict;
use warnings;
print ipv6_expand($ARGV[0]);
sub ipv6_expand
{
local($_) = shift;
chomp($_);
s/^:/0:/;
s/:$/:0/;
s/(^|:)([^:]{1,3})(?=:|$)/$1.substr("0000$2", -4)/ge; # I don't understand this line
my $temp = tr/:/:/;
s/::/":".("0000:" x (8-$temp))/e;
return $_;
}
(^|:) => the beginning of the string or a ":" character (assign to $1)
([^:]{1,3}) => followed by 1, 2 or 3 occurrences of a character other than ":" (assign to $2)
(?=:|$) => followed by a ":" character or end of string (but do not assign to $3 or anything)
"?=" is a lookahead assertion. It uses the regex (that follows it) to match, but does not "create" a group, does not "assign" any of the grouping variables.
So, "(abc)(?=d)" will match "abc" only if it is followed by "d", and then only "abc" will be set to "$1". The "d" will not be set to "$2".
In the replacement pattern:
$1.substr("0000$2", -4) => create a string by left-padding four zeros to $2 and then extracting 4 characters from the right hand side. Append this string to $1
The modifier "g" is for global match and replace.
The modifier "e" is the "evaluation" modifier. It considers the replacement text as Perl code rather than a string. It is required here in order to execute the "substr" function.