On Sun, 28 Mar 2004, 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?
> 
> 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);
>        }
>        else { $newtxt .= $_; }
>    }
>    $/ = "\n";
>    chomp $newtxt;
>    print $newtxt, "\n";
>    exit(0);
> 
> I'm also very much open to tips from the pros on this stuff.
> 
> TIA!
> 
> - Bryan
> 

The magic word is "eval". Although there are some other things in your 
code that I would "clean up", what you really need is the line:

$newtxt=eval "\"$newtxt\"";

After the 
chomp($nextxt); 

and before the 

print $newtxt,"\n";

--
Maranatha!
John McKown


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to