It seems that problem is not in Getopt::Long, but in that the substitution string has to be a plain string not escape sequence when taken from outside (of the program). Escape sequence as replacement, however, does work when hard coded in the s///. Or, something like that.

The following code -- tested in genuine xterm, bash3, perl 5.8.7, and Getopt::Long 2.68 -- will put \n as itself not as a newline if i specified the -out as an escape sequence. It did the expected when i specified the replacement string as Ctrl-J.

use warnings; use strict; use Getopt::Long qw(:config gnu_compat no_ignore_case no_debug ); # Get regexen|strings for substitution. my ($in , $out); GetOptions( 'i|in=s' => \$in , 'o|out=s' => \$out) or usage(); usage() unless defined $in && defined $out; printf "in: '%s' out: '%s'\n" , $in , $out; my $text; while (<STDIN>) { $text .= $_; } chomp $text; printf "before:\n'%s'\n" , $text; $text =~ s/$in/$out/g; printf "after:\n'%s'\n" , $text; sub usage { die "specify strings for -in & -out options.\n"; }

Update: For a user to be able to specify/use escape sequnce in replacement, use a hash, somthing like the last message in Regular Expression To Match Escape Sequences thread.

- Parv

In reply to Re: Getopt, regexen, and newlines by parv
in thread Getopt, regexen, and newlines by apotheon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.