Wow, Johns, thanks for the tips... I'm going to have to study that map command very closely. Either way you've answered my question, thanks!
Only one thing to add-- you ask: >> chomp $newtxt; >> print $newtxt, "\n"; > > Why remove "\n" in one line and then add it back on the next line? I did this because I want to make sure I end with a "\n", but I don't want an extra one if one is already there. I guess I could've also done a: $newtxt =~ s/([^\n])$/$1\n/; ... but the above seemed clearer. Is that not a good reason? - B > Bryan Harris wrote: >> >> Is there a way to interpolate strings that the user enters? >> >> I'm writing a filter (pipe-cat = pat) that lets you add text to the front or >> end of some piped data: >> >> echo "2" | pat "1" - "3\n" >> >> The "-" represents the piped data, so the above should print: >> >> % echo "2" | pat "1\n" - "3\n" >> 1 >> 2 >> 3 >> % >> >> But here's what I get: >> >> % echo "2" | pat "1" - "3\n" >> 1\n2 >> 3\n% >> >> How can I interpolate the "\n" that the user entered? > > $string =~ s/\\n/\n/g; > > >> Here's my code (disclaimer: I'm still very much a novice at this stuff). >> >> #! /usr/bin/perl -w >> >> $newtxt = ""; >> while ($_ = shift) { >> if ($_ eq "-") { >> open(FILE, "-") || die("Couldn't read from STDIN: $!\n"); >> undef $/; >> $newtxt .= <FILE>; >> close(FILE); > > Since you are reading from STDIN and it is already open: > > if ( $_ eq '-' ) { > local $/; > $newtxt .= <STDIN>; > > >> } >> else { $newtxt .= $_; } >> } >> $/ = "\n"; > > If you had used 'local $/;' above then you wouldn't have to set it here. > > >> chomp $newtxt; >> print $newtxt, "\n"; > > Why remove "\n" in one line and then add it back on the next line? > > >> exit(0); >> >> I'm also very much open to tips from the pros on this stuff. > > You could do it something like this: > > #!/usr/bin/perl > use warnings; > use strict; > > my %escapes = ( > '\\' => "\\", > n => "\n", > t => "\t", > f => "\f", > b => "\b", > r => "\r", > ); > > my $newtxt = join '', map { > s/\\([\\ntfbr])/$escapes{$1}/g; > local $/; > $_ eq '-' ? <STDIN> : $_ > } @ARGV; > > print $newtxt; > exit 0; > > __END__ > > > > John -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>