-2

I went over a piece of code in which a subroutine takes video filename as argument, and then printing it's duration time. Here I'm only showing the snippet.

sub videoInfo {
  my $file = shift;
  $file =~ s/(\W)/\\$1/g;
}

So far I understood is that it is dealing with whitespaces but I'm not able to break the meaning of code, I mean what is $1 and how it will work?

1 Answers1

1

It puts backslashes in front of non-word characters. Things like "untitled file" becomes "untitled\ file".

As in most regular expression operations $1 represents the first thing captured with (...) which in this case is the (\W) representing a single non-word character.

I think this is an unnecessary home-rolled version of quotemeta.

tadman
  • 194,930
  • 21
  • 217
  • 240