#! /bin/perl
# perlSPICE allow netlist to contain perl code!
# In the netlist:
# *$perlstart - enters perl mode
# *$perlend - exits perl mode
# In perl mode, use print() to write to output netlist

$spfile = $ARGV[0];
$spfile =~ /\.spl/ or print(STDERR "No perl filter on original spice file.\n") and exit;

$outfile = $spfile;
$outfile =~ s/\.spl/.sp/;

open(FH,$spfile);
open(OUT,">$outfile");

$perlmode=0;
foreach $ln (<FH>) {
	if ($ln =~ /\*\$perlstart\s*/) {
		$perlmode=1;
		next; 
	}
	if ($ln =~ /\*\$perlend\s*/) {
		$perlmode=0;
		$perlcode =~ s/\s*print\s*\(\s*\"/print(OUT \"/g; 
		eval $perlcode;
		$perlcode='';
		next;
	}
	if ($perlmode==1) {
		$perlcode .= $ln;
		next;
	}
	(not $perlmode) and print(OUT "$ln") and next;
}

close(FH);
close(OUT);

system "C:/synopsys/Hspice_X-2005.09/BIN/hspice.exe -i \"$outfile\"";