If you accidentally get transformed into a fly, and get caught in a Venus flytrap, here is some valuable advice: Don’t panic.

“If you just sit there and wait, the next morning, the trap will open and you can leave,” says Ranier Hedrich from the University of Würzburg. “It you panic, you induce a deadly cycle of disintegration.”

Hedrich and others have found that the Venus flytrap can count the number of times that its victims touch the sensory hairs on its leaves. One touch does nothing. Two closes the trap. Three primes the trap for digestion. And five, according to Hedrich’s latest study, triggers the production of digestive enzymes—and more touches mean more enzymes. The plant apportions its digestive efforts according to the struggles of its prey. And the fly, by fighting for its life, tells the plant to start killing it, and how vigorously to do so.

The Venus flytrap has captivated scientists for centuries, perhaps because of how un-plant-like it is. It captures and eats animals. Its leaves look unnervingly like fang-lined mouths. It moves quickly, with each of its traps closing shut in a tenth of a second. It has, on occasion, a fantastic singing voice. It is, as Charles Darwin said, “one of the most wonderful [plants] in the world.” To understand his admiration, it helps to slow things down, and see exactly what happens when the flytrap traps.

We start with a fly. It lands within the open halves of the flytrap’s trap, drawn there by the red color and fruity smell. The inner surfaces of the traps are minefields, dotted with stiff hairs. When one of these is bumped by a bumbling fly, it triggers a spike of calcium ions that send an electrical impulse through the lobes, much like those that travel along your neurons.

A single impulse means nothing; it could be triggered by wind-blown debris hitting a hair, or perhaps a falling raindrop. To avoid closing its traps for such false alarms, the flytrap is programmed to await a second signal.

The first impulse sets a secret timer, and what the fly does in the next 20 seconds will determine its fate. If it avoids the hairs, it will live. If it bumps a second one, it sets off another electrical impulse, which raises the trap’s calcium levels above a critical threshold. The plant responds by sending water into its leaves, which rapidly change shape from convex (bent outwards) to concave (bent inwards).

In other words, the trap snaps shut.

The fly is imprisoned but not dead. It struggles, knocking the trigger hairs even further and sending off more electrical impulses, around one per minute. The third impulse raises the trap’s calcium levels even further, prompting it to produce a hormone called jasmonate. In many plants, jasmonate is a touch hormone, which is released by wounds and injuries and coordinates programs of defense and repair. In the Venus fly trap, jasmonate doubles as a carnivory hormone. It primes the gland cells in the trap to start making digestive enzymes, which they finally do once they detect a fifth electrical impulse.

The plant carefully calibrates the supply of these enzymes to meet the demand for them. A large fly struggles more furiously, knocks more trigger hairs, and sets off more electrical impulses. The plant responds by making proportionally more jasmonate, and secreting proportionally more digestive enzymes.

After six to seven hours, the trap becomes hermetically sealed, and fills with fluid. The fly, cut off from the outside world and deprived of oxygen, asphyxiates—which is merciful, considering what happens next. The fluid inside the trap becomes incredibly acidic, dropping to a pH of 2. It also fills with meat-disintegrating enzymes. It turns into what Hedrich calls a “green stomach.”

The stomach takes several days to digest the dead fly, and the flytrap keeps up the assault of acid and enzymes by tasting its meal. The dead fly isn’t setting off any electrical impulses any more, but in as-yet-unpublished work, Hedrich has shown that the trap is also lined with chemical sensors. These can detect the chitin in the fly’s shell and the substances in its blood. So, as long as the plant can taste something to digest, it will keep digesting.

It also starts absorbing, doing the job of intestines as well as a stomach. The same five electrical impulses that trigger the production of digestive enzymes also activate a set of transporter enzymes, which absorb the sodium liberated from the disintegrating fly. Most plants detest salt, but the flytrap puts it to good use. By concentrating it within the traps, it can drag the water from the fly into its own tissues.

After a week or more, the trap opens, revealing the fly’s empty husk, which eventually falls out or blows away. The trap is now set for another victim. It will capture one or two more before the plant replaces it.   

Many of these details have been known for a long time, says Andrej Pavlovic from Comenius University in Bratislava. But Hedrich’s latest study finally shows that the flytrap can count electrical impulses to induce the digestive process. How it does so is still a mystery, and one that the team is actively trying to solve.

For now, it is clear that the plant is a paragon of efficiency. It ensures that it wastes no energy in capturing victims. It only closes its traps when it likely has a meal. It only starts producing fluids when it’s really sure that it has caught something. And it only continues digesting when there’s something to digest. And in doing so, it can thrive in nutrient-poor swamps and marshes where other plants struggle.