Check IPv4 address Edit on GitHub

Here is a task. You need to write a Perl sub that return 1 if its first argument is a string with a valid IPv4 address and return 0 in other case. The name of that sub should be is_valid_ipv4. The sub should not output any warnings, no matter what it gets as an argument.

Here is some examples of strings with valid IPv4 addresses:


And here is some examples of strings that are not valid:

  • Hello, world!
  • (empty string)
  • 255255255255 (no dots)
  • (the number 373 is greater that 255
  • (there should not be leading zeros before the numbers)

Here are the rules what is considered to be a string with a valid IPv4 address:

  • Only 11 symbols are allowed in the string. The digits from 0 to 9 and a dot.
  • The string must consist of 4 blocks of digits separated by dots
  • Every block of digits must produce a positive number in the range from 0 to 255 (the numbers 0 and 255 are allowed)
  • There should not be leading zeros in the beginning of a number

The code you enter will be run in perl 5.30. The code is run with use utf8;, use strict; and use warnings;. You can't use any additional libraries, you can use only libraries that are shipped with perl.