开发者

What does this perl script do? Looks tricky

New to perl scripting. Trying to understand what this does :S

@prefixes = ( "ROOT1", "ROOT2" );

$path = <>;
foreach my $prefix (@p开发者_开发问答refixes) {
    if($path =~ /\/$prefix\/(.*?)(\/|$)/ ) {
        print "$1\n";
        last;
    }
}


It prints the next directory in the input of the previous one was ROOT1 or ROOT2. examples:

/ROOT1/x/y   -> x
/ROOT1/z     -> z
/ROOT2/bla   -> bla
x/ROOT2/y/z  -> y
ROOT1/x      ->
/bla/x       -> 


It sets an array of predefined prefixes:

@prefixes = ( "ROOT1", "ROOT2" );

It then reads a path from standard input:

$path = <>;

For each prefix, it checks if the path starts with a directory name equal to the prefix:

if($path =~ /\/$prefix\/(.*?)(\/|$)/ ) {

At the same time, it collects whatever follows the prefix ((.*?)), up to the next forward slash, or up to the end ((\/|$)). If the path matched the prefix, it prints out the collected part and exits the loop:

print "$1\n";
last;

So, in short, it looks for the first prefix that matches the path, and prints the part of the path following the prefix.

Edit: "up to the last forward slash" -> "up to the next forward slash"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜