I'm no pro, but I can help a bit. You interpolate variables in a double-quote "".
my($name) = 'Adam';
print("My name is $name\n"); # Prints `My name is Adam` with a return at the end.
or more to your question
my($name); chomp($name = <STDIN>); print("His name is $name.\n");
With regard to your code, here are my changes.
#! /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);
General Comments: Comments and white space are your friend. Don't be afraid to use them to explain what you're trying to do at a certain point.
Line 3: You've declared this as a global variable. I'd recommend lexically scoping it with a `my` statement (see below for example). Also, I wouldn't waiste time initializing the variable to nothing. It defaults to undef anyway.
my ($newtxt);
Line 4: I don't get what you're trying to do here. It looks like you're using these statement as the body of a Perl script not in a subroutine. `shift` will shift @_ by default, which only exists inside a subroutine. If you are executing these statements in a stand alone script, you probably want to work with the program's arguments, which are stored in @ARGV. In a scalar context an arrray will return the number of elements, which means when you've shifted out all the elements it will return zero. A return status of zero is false in Perl. I would use logic like this to maintain the loop.
while (@ARGV) { my($param) = shift(@ARGV);
If there are no more values in an array, `shift` will return `undef` so you could also use this expression in the evaluator part of the while loop.
while ($param = shift(@ARGV)) {
As a best practice, I don't recommend storing things in $_. The reason, it confuses maintenance programers down the line.
Line 5 and 6: I would change the error message you're displaying because its not 100% accurate. Its not STDIN that is having the problem. The problem is with your new file handle `FILE`, which can't seem to open. As for that statement, you're opening a file in the working directory (the directory the program is running in) named `-`. That seems a little obscure. Some comments would be nice. What's the point of all this activity?
Who taught you about slurp mode! Its usually bad form to change $/ to undef globally (like you've done here) because it effects modules and other code that may rely on the default line read behavior. Should you be using that statement in a larger program, I'd scope it with-in a block. Now, you don't look like you're using any modules so what you have will work, but it makes you look smarter to scope the statement inside curly braces.
{ local($/) = undef; $newtext .= <FILE>; }
close (FILE);
Line 16-17: This doesn't make sense. You're removing the newline at the end of $newtxt to then add it in the next line. I'd lose the `chomp $newtxt;` statement. It really isn't surving a purpose here. Then the next statement can read `print($newtxt);`.
Line 18: `exit(0);` Bravo! I thought I was the only person that explicitly exited a script. Nice to see I'm not. This is really a matter of preference. Most of my `Perl` friends don't use it.
Regards, Adam
On Mar 28, 2004, at 1:44 PM, 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
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>