What is meant by the term hook in programming?

What is meant by the term hook in programming?

Essentially its a place in code that allows you to tap in to a module to either provide different behavior or to react when something happens.

A hook is functionality provided by software for users of that software to have their own code called under certain circumstances. That code can augment or replace the current code.

In the olden days when computers were truly personal and viruses were less prevalent (Im talking the 80s), it was as simple as patching the operating system software itself to call your code. I remember writing an extension to the Applesoft BASIC language on the Apple II which simply hooked my code into the BASIC interpreter by injecting a call to my code before any of the line was processed.

Some computers had pre-designed hooks, one example being the I/O stream on the Apple II. It used such a hook to inject the whole disk sub-system (Apple II ROMs were originally built in the days where cassettes were the primary storage medium for PCs). You controlled the disks by printing the ASCII code 4 (CTRL-D) followed by the command you wanted to execute then a CR, and it was intercepted by the disk sub-system, which had hooked itself into the Apple ROM print routines.

So for example, the lines:


would list the disk contents then re-initialize the machine. This allowed such tricks as protecting your BASIC programs by setting the first line as:

123 REM XIN#6

then using POKE to insert the CTRL-D character in where the X was. Then, anyone trying to list your source would send the re-initialize sequence through the output routines where the disk sub-system would detect it.

Thats often the sort of trickery we had to resort to, to get the behavior we wanted.

Nowadays, with the operating system more secure, it provides facilities for hooks itself, since youre no longer supposed to modify the operating system in-flight or on the disk.

Theyve been around for a long time. Mainframes had them (called exits) and a great deal of mainframe software uses those facilities even now. For example, the free source code control system that comes with z/OS (called SCLM) allows you to entirely replace the security subsystem by simply placing your own code in the exit.

What is meant by the term hook in programming?

In a generic sense, a hook is something that will let you, a programmer, view and/or interact with and/or change something thats already going on in a system/program.

For example, the Drupal CMS provides developers with hooks that let them take additional action after a content node is created. If a developer doesnt implement a hook, the node is created per normal. If a developer implements a hook, they can have some additional code run whenever a node is created. This code could do anything, including rolling back and/or altering the original action. It could also do something unrelated to the node creation entirely.

A callback could be thought of as a specific kind of hook. By implementing callback functionality into a system, that system is letting you call some additional code after an action has completed. However, hooking (as a generic term) is not limited to callbacks.

Another example. Sometimes Web Developers will refer to class names and/or IDs on elements as hooks. Thats because by placing the ID/class name on an element, they can then use Javascript to modify that element, or hook in to the page document. (this is stretching the meaning, but it is commonly used and worth mentioning)

Leave a Reply

Your email address will not be published.