Ïðèãëàøàåì ïîñåòèòü
Ãðèí (grin.lit-info.ru)

Workshop

Previous Table of Contents Next

Workshop

Quiz

1:

The first argument passed to an object method is always

  1. The class name

  2. The originally blessed reference

  3. Whatever the first argument was when you invoked the method

2:

Can you have method names with the same names as built-in functions, or of already-defined subroutines?

  1. Yes, but it's difficult, as noted in Hour 8, "Functions."

  2. Yes, and there are no concerns really.

  3. No, it's impossible.

3:

Does the constructor subroutine in a class have to be called new?

  1. Yes

  2. No

Answers

A1:

b. The reference that was blessed in the constructor is returned to each method. In this way the method gets a handle back to the object, and can use it to do its work.

A2:

b. It's not really a problem because Perl can always tell that you meant the method—and not the built-in—because it will appear on the right-hand side of a -> operator.

A3:

b. No. That's just the convention. If you use a different subroutine name, users of your class might get confused.

Activities

  • In the Q&A section, question 3 explained the AUTOLOAD method. How do you think you could construct an AUTOLOAD method that would only be able to access some of the properties within the object and not others? For a hint, see the perltoot manual page.

  • Sketch out a DirectoryInfo class. It would be a bit different than a FileInfo class. What methods would you need? What kinds of properties would you need to store?

  • The TYPFileInfoOO class recomputes the file size and line count each time the byte and lines methods are called. Is this a good idea? If you wanted to store those values so that subsequent calls would fetch the cached values instead of recomputing them each time, how would you do it?

    Previous Table of Contents Next