perl_edit
Edit files with Perl one-liners for advanced text modifications. Supports cross-platform file editing, backup creation, and multiline mode for efficient file updates without full replacements.
Instructions
Edit files using Perl one-liners (more powerful than sed, better cross-platform support)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| backup | No | Create backup file | |
| file | Yes | File to edit | |
| multiline | No | Enable multiline mode (-0777) | |
| script | Yes | Perl script (e.g., "s/old/new/g" or "$_ = uc" for uppercase) |
Implementation Reference
- src/index.ts:739-758 (handler)The main handler function for the 'perl_edit' tool. It destructures the input arguments, checks if the file exists, constructs a Perl command with options for backup and multiline processing, executes it using execAsync, and returns a success message.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:178-200 (schema)The input schema defining the parameters for the perl_edit tool: file (required string), script (required string), backup (optional boolean, default true), multiline (optional boolean, default false).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:175-202 (registration)The tool registration object for 'perl_edit', including name, description, and inputSchema, added to the tools array for MCP server registration.{ 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'] } },