lama book example not working as stated...

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 5
Reputation: 000stephen is an unknown quantity at this point 
Solved Threads: 0
000stephen 000stephen is offline Offline
Newbie Poster

lama book example not working as stated...

 
0
  #1
Dec 18th, 2008
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;
}
]
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: lama book example not working as stated...

 
0
  #2
Dec 18th, 2008
You also need to print the result, say

print "$_";
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: 000stephen is an unknown quantity at this point 
Solved Threads: 0
000stephen 000stephen is offline Offline
Newbie Poster

Re: lama book example not working as stated...

 
0
  #3
Dec 18th, 2008
thanks.
i have tried using the print statement, and so far it only prints what i enter as input.

Do you know if this code should work as written?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: lama book example not working as stated...

 
0
  #4
Dec 18th, 2008
Post your code.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: 000stephen is an unknown quantity at this point 
Solved Threads: 0
000stephen 000stephen is offline Offline
Newbie Poster

Re: lama book example not working as stated...

 
0
  #5
Dec 18th, 2008
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.
]
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: lama book example not working as stated...

 
0
  #6
Dec 18th, 2008
for right now test your program input:

  1. use strict;
  2. print "<$_>" for @ARGV;


see what gets printed. Use the code tags to post your perl code.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: 000stephen is an unknown quantity at this point 
Solved Threads: 0
000stephen 000stephen is offline Offline
Newbie Poster

Re: lama book example not working as stated...

 
0
  #7
Dec 18th, 2008
errors out with the output below. Also, the [<$_>] does not work with quotes. I can only get it to run without the quotes regardless of using [strict].

stephen@stephen:~/workspace/PerlTest$ perl -w test.pl
test.txt
readline() on unopened filehandle at test.pl line 9.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: lama book example not working as stated...

 
0
  #8
Dec 18th, 2008
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:

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: lama book example not working as stated...

 
0
  #9
Dec 18th, 2008
I believe the filename should be on the same line as the call to your perl script:

  1. perl -w test.pl test.txt
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: 000stephen is an unknown quantity at this point 
Solved Threads: 0
000stephen 000stephen is offline Offline
Newbie Poster

Re: lama book example not working as stated...

 
0
  #10
Dec 18th, 2008
thanks KevinADC.
this [perl -w test.pl test.txt] got it performing as expected.

i can now move forward.
much appreciated.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC