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

Error when trying to set, change, or use $/

Status
Not open for further replies.

zipper777

Programmer
Jan 24, 2008
5
US
I'm getting an error message whenever I try to use the variable $/. The error message that Perl spits out is:

syntax error at Z:/Workspace/PN/PN.pl line 64, near "}continue"

the code I'm using is basically the following:
Code:
sub doThings(){	
	$oldDelim = $/;
	undef $/;
	slurp things;
	$/ = $oldDelim;
	return;
}
Line 64 would be the last line (}). The odd thing is that if I use the following code then it works:
Code:
sub doThings(){	
$inFileHandle->open($filename) or die "Could not open file: " . $filename;
while (<$inFileHandle>){
	$oldDelim = $/;
	undef $/;
	print things;
	$/ = $oldDelim;
}
	return;
}
I used the EPIC debugger for Eclipse and I see that the $/ isn't listed anywhere, so it appears that it is not getting initialized, but even if I try to manually initialize it, it still does not work. I'm sure I'm just missing something obvious, but I can not seem to be able to figure it out. Can anyone offer any insight as to why this might be happening?

Thanks.

-Zipper
 
You probably have a missing semi-colon or umatched {} brackets somewhere in the code. Why not post your real code and make it easier to help you.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You seem to be trying to use a function called "slurp", but there's no such function in Perl.
 
local just defines a local copy to the enclosing block. His problem is a syntax error as the error message says:


syntax error at Z:/Workspace/PN/PN.pl line 64, near "}continue"

So his code is not even compiling if he is getting that error.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You seem to be trying to use a function called "slurp", but there's no such function in Perl.

I took it that he posted psuedo code, but if its real code then that could be a problem. But the error message he gets makes it seem like his script can't even compile so it never runs to that point anyway.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I figured out what the issue is, but I'm not sure I understand why. I had the following code which would not compile if I left any of the lines with $/ in it, but if I added an m to the line:

case /[^-].*/ {print "test2";}

to change it to:

case m/[^-].*/ {print "test2";}

then it would compile.

Code:
if (@ARGV == ""){
startInteractive();
}
while (@ARGV){
	switch (shift){
		case "-e" {print "test";}
		case /[^-].*/ {print "test2";} #assumes that if they did not put a dash at the beginning that this is the filename.
		default 	{print "default";}
	}
}

sub doThings(){
	my $filename = $_[0];
	my $inFileHandle = new FileHandle;

	$starttime = time();
	local $/;
	$oldDelim = $/;
    undef $/;
    $inFileHandle->open($filename) or die "Could not open file: " . $filename;
    $var = <$inFileHandle>;
    $/ = $oldDelim;
    print "Took: " .  time() - $starttime . " Seconds";
	$inFileHandle->close();
}

Thanks for the help

-Zipper
 
I believe the switch module documentation mentions this known problem.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Due to the heuristic nature of Switch.pm's source parsing, the presence of regexes with embedded newlines that are specified with raw /.../ delimiters and don't have a modifier //x are indistinguishable from code chunks beginning with the division operator /. As a workaround you must use m/.../ or m?...? for such patterns. Also, the presence of regexes specified with raw ?...? delimiters may cause mysterious errors. The workaround is to use m?...? instead.

from


_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top