Hullo Venerable Monks:
The program below opens a file, reads the last two lines, looks for a number, increments that number, then (I hope) appends the new lines to the bottom of the file.
When I open $file for reading (and only reading) the program works, and prints out the new line to the screen. Making it read/write (+>>$file) gives me:
Use of uninitialized value in pattern match (m//)...at line 15
I'm not sure why the simple addition of making the file read and writable empties my $line1 variable (opening with >$file destroys the file completely). Any clues?
Full code bit:
------------------------
#!/usr/local/bin/perl -w
use strict;
my($newline);
my($file) = "d:\\softdev\\Pserver.txt";
my(@lines);
my($lines);
my($line1);
my($line2);
open (DAT, "+>>$file") or die "$!";
@lines = <DAT>;
$line1 = $lines[-1];
$line2 = $lines[-2];
if ($lines[-1] =~ /(\d)/){
$newline = ($1 + 1);
print "$newline\n";
$line1 =~s/(\d)/$newline/;
print "$line1\n";
print DAT "$line1";}
close DAT;
-------------------------
(It's probably ugly code, but I'm self-taught and have no Compsci background :F)
Cheers,
Flubb