New to perl scripting. Trying to understand what this does :S
@prefixes = ( "ROOT1", "ROOT2" );
$path = <>;
foreach my $prefix (@prefixes) {
if($path =~ /\/$prefix\/(.*?)(\/|$)/ ) {
print "$1\n";
last;
}
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It sets an array of predefined prefixes:
It then reads a path from standard input:
For each prefix, it checks if the path starts with a directory name equal to the 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: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”