Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-06-2011, 08:21 PM   PM User | #31
joeDetroit
New Coder

 
Join Date: Nov 2011
Location: Detroit
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
joeDetroit is an unknown quantity at this point
Last question--

I'm using file locking (multiple builds going on at once, so I don't want collisions)

If I put
Code:
flock ($newlicense_fh, 2)
in this block
Code:
open my $newlicense_fh, '>', $newFile or die "failed to open '$newFile' <$!>";
			flock ($newlicense_fh, 2)
			foreach my $hostname ( keys %licenses ) {
				foreach my $license ( @{ $licenses{ $hostname } } ) {
					print {$newlicense_fh} "$hostname $license\n";
					}	
				}
			close $newlicense_fh;	
			}
}
I get
Code:
"my" variable $hostname masks earlier declaration in same statement at k:\Packages\Packages\MirrorFolder4.1\autoAssign.pl line 50.
syntax error at k:\Packages\Packages\MirrorFolder4.1\autoAssign.pl line 49, near "$hostname ("
syntax error at k:\Packages\Packages\MirrorFolder4.1\autoAssign.pl line 50, near "} ) "
Global symbol "$newlicense_fh" requires explicit package name at k:\Packages\Packages\MirrorFolder4.1\autoAssign.pl line 51.
Execution of k:\Packages\Packages\MirrorFolder4.1\autoAssign.pl aborted due to compilation errors.
If I remove the lock, the script works.

I'm using a shared lock when the files is read, and an exclusive lock when the new file is written.

From what I saw about locking, I (think) I did it right, but apparently not.

Is there a better way? Should I use lockf or something else?

Thanks!

Full code:
Code:
#!/usr/bin/perl

use warnings;
use strict;
use File::Copy;
use Data::Dumper;

my $path   = 'k:/Packages/Packages/MirrorFolder4.1';
my $file   = "$path/testlicense.txt";
my $backup = "$path/testlicense.bak";
my $newFile = "$path/new-license.txt";

my $strSearchFor = "unassigned";
my $setCount = 0;
my %licenses;

open my $license_fh, '<', $file or die "failed to open '$file' $!";
flock ($license_fh, 1);

#read file in
LICENSE:
while ( my $line = <$license_fh> ) {
    chomp $line;
    next LICENSE if $line =~ /^\s*$/;
    my ($hostname, $license) = split / /, $line, 2;
    unless ($hostname and $license) {
        print Dumper $line;
        next LICENSE;
    }
    push @{ $licenses{$hostname} }, $license;
}
close $license_fh;

move($file, $backup) or die "copy failed: $!"; #make a backup of the license file

#look for the word 'unassigned' and replace the first one with the computer name
while($setCount == 0){
	print "while loop...\n";
	print "Found so far- " . $setCount . "\n";
		if ( exists $licenses{$strSearchFor} ) { 
			print "found unassigned license key...\n" ;
			$licenses{ $ENV{'COMPUTERNAME'} } = [shift @{ $licenses{ 'unassigned' } }];
			print "substitued current computer name...\n";
			$setCount = 1;
			print "Found so far- " . $setCount . "\n";
			print "opening new license file for writing...\n";
			open my $newlicense_fh, '>', $newFile or die "failed to open '$newFile' <$!>";
			flock ($newlicense_fh, 2)
			foreach my $hostname ( keys %licenses ) {
				foreach my $license ( @{ $licenses{ $hostname } } ) {
					print {$newlicense_fh} "$hostname $license\n";
					}	
				}
			close $newlicense_fh;	
			}
}
joeDetroit is offline   Reply With Quote
Old 12-06-2011, 08:24 PM   PM User | #32
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
You're missing the semi-colon at the end of the flock statement.
FishMonger is offline   Reply With Quote
Old 12-06-2011, 08:27 PM   PM User | #33
joeDetroit
New Coder

 
Join Date: Nov 2011
Location: Detroit
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
joeDetroit is an unknown quantity at this point
Quote:
Originally Posted by FishMonger View Post
You're missing the semi-colon at the end of the flock statement.
-derp- Thanks!
joeDetroit is offline   Reply With Quote
Reply

Bookmarks

Tags
perl, replace, text

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:00 PM.


Advertisement
Log in to turn off these ads.