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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl says there is a syntax error but I can't find it, help please. 1

Status
Not open for further replies.

plasmatwin

Programmer
Nov 1, 2005
31
GB
ok I have the following subroutine in one of my programs, all it does is read a file and check various things then load it as a config. The subroutine isn't finished yet but this part of the syntax should be fine :-/

I have highlighted the parts that perl says are wrong in [red]red[/red] and I've put the error message at the bottom of this post.

Code:
sub _rehash
{
	print "Loading config...\n";
	my $config="./battleserv.conf";

	# check for config readability/existance
	#if (!-r -e $config)
	#{
	#	print "ERROR: Config file does not exist/is not readable\n";
	#	return "Failed to load config, please check the above for possible errors";
	#}
	# read the config file into an array and then close it
	open(CONFIG, $config) or
	{
		print "ERROR: Could not open config file, config not loaded\n";
		[red]return[/red] "Failed to load config, please check the above for possible errors";
	}
	my @config=<CONFIG>;
	close(CONFIG);
	# chomp those naughty naughty line breaks
	chomp(@config);

	my $seen_warning;

	# first read through will check the config file by passing it into the hash "%check_config"
	# and then seeing if required values are there aswell as checking sanity of other values
	my %check_config;
	my @check_channels;
	foreach (@config)
	{
		(my $config, my $value) = split(/=/, $_, 2);
		if ("$config" eq "channel")
		{
			# we put the channels into an array after sanitising them so that if everything passes
			# without any critical errors we can just copy the array into the actual array
			push(@check_channels, "$value");
		}
		else
		{
			# we will read these into a temp hash called %checkconfig this way we can perform
			# sanity checks on every part of the hash
			if ($check_config{$config}) {
				$seen_warning = 1;
				print "WARNING: repeat value of $config in config file (current value: $check_config{$config}), replacing with newest value ($value)\n";
			}
			$check_config{$config} = "$value";
		}
	}

	if ($seen_warning)
	{
		print "Warnings were flagged in the config, while this still has produced a working config there is the possibility that the config will not perform the way you expect.\nIf the bot is not operating the way you expect then check the above for possible errors in your config.\n";
	}
	# clean the arrays out
	undef %config;
	undef @channels;
	# since we have two nicely sanitised arrays we just have to copy them into the global arrays
	@channels = @check_channels;
	%config = %check_config;
	print "Config loaded successfully\n";
[red]}[/red]
and the error...
Code:
syntax error at C:\BattleServ\battleserv.pl line 66, near "return"
syntax error at C:\BattleServ\battleserv.pl line 111, near "}"
Execution of C:\BattleServ\battleserv.pl aborted due to compilation errors.

I have NO idea why these errors are here... I suspect it is because I am being an idiot and not spotting something but whatever... any help appreciated :)
 
Here it is:

Code:
 open(CONFIG, $config) or
    {
        print "ERROR: Could not open config file, config not loaded\n";
        return "Failed to load config, please check the above for possible errors";
    }
You're trying to use a multiline block of code after your `or' operator. You can't do it that way.
 
oh... I didn't know that >_><_<

thanks... er.. any way I can fix this so I still execute both statements?
 
This'd be one way.

Code:
    unless( open(CONFIG, $config) ) {
        print "ERROR: Could not open config file, config not loaded\n";
        return "Failed to load config, please check the above for possible errors";
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top