Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print() on closed filehandle LOG 1

Status
Not open for further replies.

massoo

Technical User
Aug 18, 2003
7
0
0
IN
hi all,

i am getting this error in my perl code. I am not a programmer and need to have this error sorted out. The script's job is to take up a weekly backup and print the revision no. into a log file. The error thrown at the console is
print() on closed filehandle LOG at /home/shann/weekbkp.pl line 20.

The code is as follows: I have highlighted the line 20.
#!/usr/bin/perl -w
#
# Perform a weekly backup of a Subversion repository,
# logging the most-recently-backed-up revision so an
# incremental script can be run other days.

my $svn_repos = '/cvs/satrang/software';
my $backups_dir = '/home/svnbkp';
my $next_backup_file = 'weekly-full-backup-svn.' . `date +%Y%m%d`;

my $youngest = `svnlook youngest $svn_repos`;
chomp $youngest;

print "Backing up to revision $youngest\n";
`svnadmin dump $svn_repos > $backups_dir/$next_backup_file`;
print "Compressing dump file...\n";
print `gzip -9 $backups_dir/$next_backup_file`;

open(LOG, '>$backups_dir/last_backed_up');
[COLOR=red yellow]print LOG '$youngest';[/color]
close LOG;
 
Code:
open LOG, ">$backups_dir/last_backed_up" or die "can't open logfile: $!\n";
print LOG $youngest;
Use double quotes for variable interpolation and check if open was successful.
 
hi knights199,

you are truely knight. now i have one more script which does the daily backup. the errors thrown when the script when executed are:

Useless use of a constant in void context at /home/shann/daybkp.pl line 26.
Name "main::svnadmin_cmd" used only once: possible typo at /home/shann/daybkp.pl line 23.
Name "main::next_backup_file" used only once: possible typo at /home/shann/daybkp.pl line 7.
Name "main::svn_repos" used only once: possible typo at /home/shann/daybkp.pl line 5.
readline() on closed filehandle IN at /home/shann/daybkp.pl line 9.
Use of uninitialized value in scalar chomp at /home/shann/daybkp.pl line 10.
Use of uninitialized value in string eq at /home/shann/daybkp.pl line 14.
Use of uninitialized value in addition (+) at /home/shann/daybkp.pl line 20.
Backing up revisions 1 to svnlook youngest $svn_repos...
Compressing dump file...
gzip -9 $backups_dir/$next_backup_file


the script with the highlighted lines is

#!/usr/bin/perl -w
# # Perform a daily backup of a Subversion repository,
# using the previous most-recently-backed-up revision
# to create just an incremental dump.
[COLOR=red yellow]$svn_repos = '/cvs/satrang/software';[/color]
$backups_dir = '/home/svnbkp';
[COLOR=red yellow]$next_backup_file = 'daily-incremental-backup-svn.' . 'date +%Y%m%d' ;[/color]
open(IN, '$backups_dir/last_backed_up');
[COLOR=red yellow]$previous_youngest = <IN>;
chomp $previous_youngest;[/color]
close IN;
$youngest = 'svnlook youngest $svn_repos' ;
chomp $youngest;
[COLOR=red yellow]if($youngest eq $previous_youngest) {[/color]
print "No new revisions to back up.\n";
exit 0;
}
# We need to backup from the last backed up revision
# to the latest (youngest) revision in the repository
[COLOR=red yellow]$first_rev = $previous_youngest + 1;[/color]
$last_rev = $youngest;
print "Backing up revisions $first_rev to $last_rev...\n";
[COLOR=red yellow]$svnadmin_cmd = 'svnadmin dump --incremental ' .
'--revision $first_rev:$last_rev ' .
'$svn_repos > $backups_dir/$next_backup_file';[/color]
[COLOR=red yellow]'$svnadmin_cmd' ;[/color]
print "Compressing dump file...\n";
print 'gzip -9 $backups_dir/$next_backup_file' ;
open LOG, ">$backups_dir/last_backed_up";
print LOG $last_rev;
close LOG;
 
Double quotes cause the string to be interpolated (e.g. fill in scalars)
Single quotes make a literal string. No interpolation here.
Code:
print '$foo';
outputs as $foo, not the value in $foo.
Backticks ` interpolate the string and run it as a system command and return the value returned by the system command.
If you don't care about the return value, use the system() call.

I think you can easily solve this yourself, it's merely a matter of putting in the right quotes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top