Steve Hemond wrote: > > As you know, I am trying to insert a string at the beginning of a file > without overwriting its contents. Since my file is an HP/GL2 output > file, I put every hp/gl2 command and stack them into an array. I then > truncate the file, insert my specific string, and then put back all the > commands after that string : > > open (FILE, "+>>$ARGV[0]") or die ("Can`t open file! : $!\n");
Why is the end of your current file a safer place to write than a separate, new file? If you explained then I'm sure we could find an easier way. I've never seen code that needed to be written like this. Are you just looking for something that needs to be atomic? (Without a separate process being able to see anything but the 'before' or the 'after')? > # We put contents of the file in a temporary scalar > my $in = <FILE>; > > # Clear the file > truncate (FILE,0) or die ("Can't truncate file! : $! \n"); Without any locking, this file could have been read by a separate process either before, after, or even (if you're unlucky with your filing system) half-way through the truncation. Writing code this way will only confuse your solution and won't protect in any way. Two of us, including me, asked exactly what you meant by, 'without overwriting its contents'. Why won't you tell us? > # Build the string to insert at the beginning > my $jobname = $ARGV[1]; #ARGV[0] is the file to be modified > my $pjl = '@PJL JOB NAME = "' . $jobname . '"' . '\n'; # What a dumb way to > build the string, any better ideas? my $pjl = "\e%-12345X [EMAIL PROTECTED] JOB NAME = \"$jobname\"\n"; > printf FILE "\e%-12345X "; > print $pjl; You see, here you are, 'overwriting' the file, which you said you mustn't do. > As you can see, I wasn't able to build the string like this : > printf FILE "\e%-12345X @PJL JOB NAME = $ARGV[1] \n"; > > Because between double quotes the @ of @PJL will be interpolated, which > I don't want. > > When running the script I get this error : > Use of uninitialized value in printf at ./hptable.pl line 126, <FILE> > line 1. > line 126 = printf FILE "\e%-12345X "; I can't spot any problem, but you need 'print' instead of 'printf' here. I suspect the real source code is different from what you've shown. My Perl runs this single line fine. > I'd like first to know a better way to build the string (avoiding the @ > interpolation). My above (again): my $pjl = "\e%-12345X [EMAIL PROTECTED] JOB NAME = \"$jobname\"\n"; is the nicest way I can think of. Single quotes will let you use '@' and '%' literally, but also prevent you from interpolating the scalar $jobname and using "\e" and "\n". > Then I'd like to know why I get that error when printing the string in > the file. Not yet, but I HTH in some way. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>