perl_edit
Edit files using Perl one-liners for text manipulation with cross-platform support, enabling targeted modifications without full file replacement.
Instructions
Edit files using Perl one-liners (more powerful than sed, better cross-platform support)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file | Yes | File to edit | |
| script | Yes | Perl script (e.g., "s/old/new/g" or "$_ = uc" for uppercase) | |
| backup | No | Create backup file | |
| multiline | No | Enable multiline mode (-0777) |
Implementation Reference
- src/index.ts:739-758 (handler)The handler function for the perl_edit tool. It executes a Perl one-liner script on the specified file, with options for backup and multiline processing.case 'perl_edit': { const { file, script, backup = true, multiline = false } = args; if (!existsSync(file)) { throw new Error(`File not found: ${file}`); } const backupExt = backup ? '.bak' : ''; const multilineFlag = multiline ? '-0777 ' : ''; const perlCmd = `perl -i${backupExt} ${multilineFlag}-pe '${script}' '${file}'`; await execAsync(perlCmd); return { content: [{ type: 'text', text: `Successfully edited ${file} using Perl${backup ? ' (backup created as .bak)' : ''}` }] }; }
- src/index.ts:175-202 (registration)Registers the perl_edit tool in the listTools response, including its name, description, and input schema.{ name: 'perl_edit', description: 'Edit files using Perl one-liners (more powerful than sed, better cross-platform support)', inputSchema: { type: 'object', properties: { file: { type: 'string', description: 'File to edit' }, script: { type: 'string', description: 'Perl script (e.g., "s/old/new/g" or "$_ = uc" for uppercase)' }, backup: { type: 'boolean', default: true, description: 'Create backup file' }, multiline: { type: 'boolean', default: false, description: 'Enable multiline mode (-0777)' } }, required: ['file', 'script'] } },
- src/index.ts:178-200 (schema)Input schema definition for the perl_edit tool, specifying parameters and types.inputSchema: { type: 'object', properties: { file: { type: 'string', description: 'File to edit' }, script: { type: 'string', description: 'Perl script (e.g., "s/old/new/g" or "$_ = uc" for uppercase)' }, backup: { type: 'boolean', default: true, description: 'Create backup file' }, multiline: { type: 'boolean', default: false, description: 'Enable multiline mode (-0777)' } }, required: ['file', 'script']
- src/index.ts:383-408 (helper)Help content and examples for the perl_edit tool.perl_edit: `perl_edit - Perl one-liner execution =================================== Direct access to perl's text processing power. Examples: // Simple substitution perl_edit({ file: "data.txt", script: "s/foo/bar/g" }) // Delete lines perl_edit({ file: "log.txt", script: "$_ = '' if /DEBUG/" }) // Transform to uppercase perl_edit({ file: "names.txt", script: "$_ = uc" }) // Complex multiline operations perl_edit({ file: "code.js", script: "s/function\\s+(\\w+)\\s*\\(/const $1 = (/g", multiline: true }) Tips: - Use $_ for the current line - Escape backslashes in regex - multiline mode slurps entire file `,