| | |
lama book example not working as stated...
Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2008
Posts: 5
Reputation:
Solved Threads: 0
Thanks in advance for reading this, and commenting.
The example below is from p.144 in the latest Lama book edition (O'Reilly 2008), and I can't get it to behave as the book states. It states that the diamond (<>) operator will take user input (which should be a file name), and run the search-n-replace against it. If a match is found, the [$^I] variable will create a new file with ".bak" appended to the file name, which will hold the output of the find-n-replace.
When I run the script, enter a file name (file is a text file in same directory containing the word "Author:" on a new line), nothing happens, and no file is created.
I'm beginning to think I'm missing something more fundamental than syntax. My question is, does this code work, and is it stated correctly?
I've tried running this on XP and Ubuntu, with the same effect.
[code = perl 5.010]
[
use strict;
chomp(my $date = localtime);
$^I = ".bak";
while (<>)
{
s/^Author:.*/Author: new author name;
}
]
The example below is from p.144 in the latest Lama book edition (O'Reilly 2008), and I can't get it to behave as the book states. It states that the diamond (<>) operator will take user input (which should be a file name), and run the search-n-replace against it. If a match is found, the [$^I] variable will create a new file with ".bak" appended to the file name, which will hold the output of the find-n-replace.
When I run the script, enter a file name (file is a text file in same directory containing the word "Author:" on a new line), nothing happens, and no file is created.
I'm beginning to think I'm missing something more fundamental than syntax. My question is, does this code work, and is it stated correctly?
I've tried running this on XP and Ubuntu, with the same effect.
[code = perl 5.010]
[
use strict;
chomp(my $date = localtime);
$^I = ".bak";
while (<>)
{
s/^Author:.*/Author: new author name;
}
]
•
•
Join Date: Dec 2008
Posts: 5
Reputation:
Solved Threads: 0
I have made one deviation from the books example. I use [localtime] to get the date stamp, as opposed to the books statement [my $date = `date`], which I could not get to work.
/*** perl example p.144 ***/
[
chomp(my $date = localtime);
$^I = ".bak";
while(<>)
{
s/^Author:.*/Author: NewAuther Name/;
s/^Phone:.*\n//;
s/^Date:.*/Date: $date/;
print;
}
]
/*** content of test.txt ***/
[
Author: Some Dude
Phone: 123-321-1234
Date: way back in time, but definitely not today.
]
/*** perl example p.144 ***/
[
chomp(my $date = localtime);
$^I = ".bak";
while(<>)
{
s/^Author:.*/Author: NewAuther Name/;
s/^Phone:.*\n//;
s/^Date:.*/Date: $date/;
print;
}
]
/*** content of test.txt ***/
[
Author: Some Dude
Phone: 123-321-1234
Date: way back in time, but definitely not today.
]
for right now test your program input:
see what gets printed. Use the code tags to post your perl code.
Perl Syntax (Toggle Plain Text)
use strict; print "<$_>" for @ARGV;
see what gets printed. Use the code tags to post your perl code.
You did not use the code I posted. Just use the code I posted, only the code I posted and see what is printed. If this line does not work with the quotes:
than your install of perl is no good. Please start using the code tags to post your perl code.
Perl Syntax (Toggle Plain Text)
print "<$_>" for @ARGV;
than your install of perl is no good. Please start using the code tags to post your perl code.
Last edited by KevinADC; Dec 18th, 2008 at 7:36 pm.
I believe the filename should be on the same line as the call to your perl script:
Perl Syntax (Toggle Plain Text)
perl -w test.pl test.txt
![]() |
Other Threads in the Perl Forum
- Previous Thread: How to restrict WMI ports are random(Use Perl)?
- Next Thread: Out of memory With PERL on a CYGWIN/ Win 2K3 machine
| Thread Tools | Search this Thread |






