Exercises

  1. Write a subroutine that swaps two arrays.

  2. Write a subroutine that verifies that the argument is a correctly formed integer. For extra credit, give it a flag that lets it check for integers; floating-point numbers (with and without exponents); complex, hexadecimal, and octal numbers; or any combination of these.

  3. Write a module to count the words, lines, and characters in a string.

  4. Write a module to print a hash nicely. Handle the case when the values contain new lines. (For extra credit, handle the case when the value contains unprintable characters.)

  5. Write a function to encode a string so that it may be enclosed in double quotes safely. For example:

    This "string" contains a backslash (\)
    becomes
     "This \"string\" contains a backslash (\\)" ...

Get Perl for C Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.