The poet Mary Robinson was, said Coleridge, a woman of undoubted Genius. She published her first book while a child bride in a debtors’ prison; she was a political radical who took the future George IV as a lover; in portraits her eyes are serious and her mouth is not. But sickness being no respecter of even the most fascinating people, she acquired an infection at the age of 26, and afterwards lived with paralysis and pain. One night in Bath, finding her suffering intolerable, she dosed herself with 80 drops of a tincture of alcohol and opium, and drowsily composed a poem called “The Maniac”, “like a person talking in her sleep”. Inspired by the memory of a vagrant, it is not a work on which to pin a reputation, but has a place in the history of letters as the first of the English Romantic opium poems. In my Puritan youth I held the cult of the drug-addled artist in contempt. Thomas De Quincey in his voluminous sleeves? A sap, I thought, of doubtful moral fibre. William S Burroughs? What did I want with a man who shot his wife? Besides, Naked Lunch was nothing like as nasty as it thought it was. If marijuana had caused Jack Kerouac’s sentences to be as affectless as the rap of a fork on a Formica table, it was a pity he hadn’t confined himself to tobacco. Susan Sontag wrote on speed: this I admired, since it indicated a solid work ethic. I adored Coleridge, but flinched from the thought of him in the arms of Morpheus as I’d flinch from seeing my father naked. Secretly I admired Middlemarch’s Casaubon, whose ascetic and studious life was directed towards “thoroughness, justice of comparison, and effectiveness of arrangement”.

Facebook Twitter Pinterest Sarah Perry. Photograph: Graeme Robertson

Much later – my youth behind me; my Puritan strain softened but still present – I began to write my third novel. It was inspired by Charles Robert Maturin’s Melmoth the Wanderer, and in it a guilt-stricken woman encounters the myth of a cursed being wandering the world bearing witness to folly and failure. Like Casaubon, I wanted my comparisons to be just, and my arrangements effective. I was in some respects a prig, I suppose, and prided myself on my priggishness, and the pride preceded a fall; for what I have on my hands now is a novel that was written while high.

Melmoth is not an opium novel – it is nothing so Romantic as that – but it is certainly an opioid one, and a gabapentin one, and a diazepam one: it is an amitriptyline and clonazepam novel, and if it did not quite come to me in a dream, I sympathise with John Milton’s Eve, who had something devilish squat by her ear like a toad listing “phantasms and dreams”. In the aftermath of writing it I have come to understand literary drug culture as being more properly a culture of pain, and the relieving of it; of works written under the influence both of suffering and the doped-up euphoria of respite. “My sole sensuality,” Coleridge said, “was not to be in pain.” He had eye infections and a gammy knee. Percy Bysshe Shelley had headaches; De Quincey had trigeminal neuralgia; Kurt Cobain took heroin for stomach pain. If others applied their medicine to some other kind of pain – that of boredom, say, or misery – it seemed hardly my place to cavil that this suffering had been deserving of relief, and not that.

In the autumn of 2016, my body weakened by chronic illness, a disc ruptured in my lower spine. My left leg, oblivious to signals from a compressed nerve, weakened and dragged, and my foot burned as if I’d stood in the sun at noon. The pain was considerable, but tolerable. Some months later, the disc ruptured again. In my MRI scan, white streaks of muscle wastage framed the black pulp of leaking disc matter, which was pushing at the sciatic nerve like a cellist’s finger on a string. Seeing it, the inscrutable doctor softened. You must be in torment, he said. How would I rate my pain, from one to 10? He looked at me. I was beyond speaking. Ten, he said. Ten. He made a note.

Facebook Twitter Pinterest Thomas de Quincey. Photograph: Hulton Archive/Getty Images

The world dwindled down to the mattress on which I lay and sometimes howled. In a Tupperware box on the dining table (out of reach, because my husband understood that either by mistake or resolve an overdose was possible), were drugs to prevent muscle spasm, and to reduce nerve pain; there were drugs that mimicked opium, and drugs to reduce inflammation; there were drugs to flood me with serotonin and aid sleep. With anxious exactitude he recorded each dose in a small black book, and I came to know each pill. Green-and-yellow tramadol, busying itself about my limbic system and my spinal cord, could cause my head to float above the pillow. Fistfuls of Neurontin came in capsules the colour of clotted cream, and when I could walk gave me the reeling gait of a disembarked sailor. I despised the chalky dots of codeine that made me sick, but longed for the hour when I could be dosed with so large a quantity of diclofenac that it could not be administered orally. Diazepam (20mg thrice daily and never soon enough) caused in me a kind of weary resignation, and slowed my speech to that of someone half-sleeping. I was – for the first time in my life, and for some weeks, if not constantly – more or less out of my mind. I recall once dividing in two, so that my old sober self stood, walked into the corner, and looked down aghast at the figure on the mattress that rocked and groaned: who was she? Her hair was filthy. She was not dressed. She ought to be ashamed of herself.

The problem with describing pain, of course, is that you can no more know what I mean by torment than I can know what you mean by love – and besides, privately we all think ourselves made of sterner stuff than the sickly. Poorly writers with time on their hands are, I suspect, rather a bore, and even more so when high. They plead their case; they become their own subject. Perhaps it is this, as much as the effusion of opium, that has created the body of work written under the influence. Shelley, who carried his laudanum about in a flask, considered freedom from pain as far from human grasp as flight: what could the skylark sing about but love of its own kind and “ignorance of pain?” Coleridge in “Sonnet: Composed in Sickness” wrote that “Tyrant Pain had chased away delight.” No wonder he retreated into laudanum: it unlocked the gates to Xanadu when all other gates were bolted.

No wonder he retreated into laudanum: it unlocked the gates to Xanadu when all other gates were bolted

In solitary confinement on my mattress I devised a taxonomy of pain. Virginia Woolf, prone to the flu, bemoaned the poverty of language for pain in English. How could the woman who saw the tolling of Westminster’s bells dissolve like leaden circles in the air not have known the whole thesaurus of suffering, and made entries of her own? Perhaps she could have done with 80 drops of laudanum. Galen, the Greek physician, had identified four kinds of pain – the pulsating, the lancinating, the weighty and the stretching – and Avicenna expanded the canon in the 11th century: there is the soft, the breaking, the coarse, the penetrating, the bitter and the numb. With my imagination unloosed by opioids and nothing else to do, I conceived of ways to describe my pain. Muscle spasms had me in a vice that a quiet man turned with the implacable strength of a carpenter working oak. Nerve pain was dialysis that replaced my blood with thin and icy fluids. Sometimes only the cheapest of metaphors would do. Pain was maddening: once, when I had exhausted my ration of painkillers, I beat at my leg with clenched fists. By then I had only the opening pages of my novel in hand, and months of research into atrocity and genocide behind me. The fruit of that study went rotten on the branch, because I could not put it to use. I tried to dictate to a laptop on the floor by my pillow, but what appeared on the screen was a poor effort, and in any case I could barely read. The urge to write became a question of lancing a boil: I was full of poisonous matter and needed to let it out.

Facebook Twitter Pinterest William S Burroughs. Composite: Rex/Alamy

I did not hallucinate. No monsters jostled my mattress unless summoned. I have never been one of those writers who say: “I saw him, this character of mine – he was standing on a station platform with a red coat and he bared his teeth at me.” My characters are plot devices that do as they’re damn well told. But in those drugged months the monsters I summoned were vivid and ghastly. De Quincey lamented opium’s effects of intellectual torpor, but described with pleasure “the reawakening of a state of eye generally incident to childhood”. Did his reader know, he said, that children “have a power of painting, as it were upon the darkness, all sorts of phantoms”?

I regained the eye of childhood. One night a pale girl stood in my room in a party dress and frilled white socks. In Prague an opera house filled with jackdaw feathers, and somebody somewhere was burning alive.

Might I have written a sober affair, had I not been under the influence? Perhaps not – I have never needed tramadol to be attended by angels, or to feel demons pricking my feet. But I think of Vincent van Gogh, who looked at the world through the yellowish haze conveyed by digitalis, and grew enraptured by sunflowers and straw chairs, and I think of a glass prism through which a beam of white light passes and is split into a rainbow. What had been a single lucid idea had passed through the drugs I took and been dispersed into a spectrum of colours I had only half foreseen.

Facebook Twitter Pinterest Mary Robinson. Photograph: Universal History Archive/UIG via Getty Images

In the 18th century, East Anglians worn out by working on the fens called laudanum “elevation”. How apt! If I was not exactly happy, a curious kind of stoicism descended on me. I was willing to look horrors both real and imagined dead in the eye, which can be nothing but useful to the novelist. At about that time I had burned the deadened skin of my leg with a hot-water bottle, and was left for weeks with a necrotising hole. Twice a day I peeled back the dressing and sluiced the wound with saline and did it quite cheerfully and disinterestedly, as if I were attending to someone else. I was tranquillised, a little euphoric. And the book was full of burning: with acid, with petrol, with bundles made of young green wood to make the burning slower. Later, weaned off the tramadol and the diazepam, I could not bear to look, and would plead with others to do it for me.

When in time my pain outgrew the contents of the Tupperware box, my GP said: next, we try morphine. I was appalled. I did not want to be one of Alfred, Lord Tennyson’s “mild-eyed melancholy lotus-eaters”. Tramadol is cousin to heroin, but morphine is her sister. I would strangle a dog, I thought, for respite, but I drew the line at smack.

The shame I obscurely felt, and the notion it would be immoral to pass idle months drowsily sucking at a bottle of opiates, was a modern anxiety. Mary Robinson had not felt she was transgressing; Jane Austen’s mother had recommended opium for travel sickness; Louisa May Alcott took opium after a bout of typhoid fever.

In 1851 the Cyclopaedia of Six Thousand Household Recipes provided instructions for opium remedies for sleepless babies. Opium, it suggested, may be mixed with quince and oils of cinnamon, cloves, nutmeg and mace. Opium enemas are advised. Coleridge relied on the potent Kendal Black Drop, which included treacle and vegetable oil to aid its digestion. The drug itself carried no stigma – but an excess of it, as with an excess of anything, was a disgrace. Coleridge felt the shame deeply and in 1806, in Malta, he wrote: “I have never sought pleasure for its own sake, but only as a means of escaping the pains that coiled around my mental powers, as a serpent around the body and wings of an Eagle!”

My own person from Porlock came in the form of Mr Rai, a Norwich neurosurgeon, who intervened before morphine’s last resort. He opened my back and released the crushed sciatic nerve from its tormentor and I was transported into a world in which walking and sitting and coughing and turning did not cause me to wonder if death, on the whole, would be preferable. The morning after surgery he stood lightly stroking my feet. Feel this, he asked. I could. I had not been paralysed. There is nothing, I said. There is no pain, there is nothing, I am in no pain at all! He laughed, and said that I was (a clear tube drained blood from my flank into a glass flagon set on the floor), but that it simply did not register. It was too mild, in comparison: the parameters had shifted.

I am no longer elevated but the doors that opened do not seem to have been firmly bolted. Months later, in the attic of a house in St Andrews, under the influence of nothing but chamomile tea, I felt the covers slip from my feet and the whole bed sink while floorboards creaked underfoot. I consulted the Sunday School lessons stored on the shelves of my mind. The Bible did not provide, strictly speaking, for ghosts; but advised a simple stern command for the banishment of demons. This, I felt, would hardly suffice: the bedcovers slipped again. I sang Psalm 23 until I fell asleep, mocking myself a little, but to no effect.

I have never sought pleasure for its own sake, but only as a means of escaping pains that coiled around my mental powers Samuel Taylor Coleridge

Casaubon-like, sober, ascetic in my habits, I turned diligently to the novel. Permitted to sit for short periods at my desk I worked and reworked the manuscript. The sleep of reason had bred monsters that required the red pen. I cut, and pasted, and jostled, and scowled; in time it became impossible to judge what had been written before the pain, what during, and what after.

Sometimes I reflect on that time – the mattress, the pills and the little black book, the taxonomy of pain – and cheerfully concede that it was not a waste. I had resolved to write a novel that would force the reader to bear witness to wickedness, and it had demanded that I bear witness too. I had been given a brief period of suffering against which to measure the suffering of others, and sometimes the courage – dispensed each fortnight by the supermarket pharmacist – to face both. Grateful for my good fortune at escaping pain, and escaping with nothing to show for it but two scars and a slight limp, I illuminated the dark materials of my research and my dreams with the promise, or at least the possibility, of redemption.

Wherever I go I carry a little beaded bag containing my few remaining tramadol, a strip or two of diazepam. I do not need them – I am quite well; I sometimes run; I sleep easily – but all the same … “Today I shall sing more sweetly,” said Coleridge, “but the tomorrow is yet to come.”

• Melmoth is published by Profile. To order a copy for £14.61 (RRP £16.99) go to guardianbookshop.com or call 0330 333 6846. Free UK p&p over £10, online orders only. Phone orders min p&p of £1.99.