Changing text via a Services-based Perl shell scripts in Shortcuts on macOS 12 Monterey

I figured something similar out. The missing piece seemed to be adding a Stop and output action to the end. The wrinkle: this only works after a JXA action (Run JavaScript for Automation), not a Run Shell Script action.

The final Shortcut

The code:

function run(input, parameters) {
    const text = input[0][0];
    const lines = text.split('\n');
    const prefixedLines = lines.map(l => `> ${l}\n`);
    return ''.concat(...prefixedLines);
}

This Shortcut doesn't keep the text selected after it's done running (unlike the Automator action), but that's not a dealbreaker for me.

Additionally, one can just modify input[0][0] and return the entire input variable, but this works just as well for my purposes.