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.

[ use strict;

chomp(my $date = localtime); $^I = ".bak";

while (<>) { s/^Author:.*/Author: [I]new author name[/I]; } ][code = perl 5.010]
[
use strict;

chomp(my $date = localtime);
$^I = ".bak";

while (<>)
{
s/^Author:.*/Author: new author name;
}
]

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?

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.
]

Edited 3 Years Ago by mike_2000_17: Fixed formatting

for right now test your program input:

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

see what gets printed. Use the code tags to post your perl code.

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.

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:

print "<$_>" for @ARGV;

than your install of perl is no good. Please start using the code tags to post your perl code.

I believe the filename should be on the same line as the call to your perl script:

perl -w test.pl test.txt

thanks KevinADC.
this [perl -w test.pl test.txt] got it performing as expected.

i can now move forward.
much appreciated.

This article has been dead for over six months. Start a new discussion instead.