Skip to content

Instantly share code, notes, and snippets.

@h0h0h0
Created March 9, 2010 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h0h0h0/326800 to your computer and use it in GitHub Desktop.
Save h0h0h0/326800 to your computer and use it in GitHub Desktop.
#utility to replace every "$commandApi->Invoke(foo, bar, baz)" with "print "$commandApi->Invoke(foo, bar, baz)""
#for debugging
#just outputs to stdout; redirect to a file to pop it into an actual file
# $commandApi->Invoke('Construct', "$key" );
my $pathToInputFile = shift;
#to generate code, change $codeGenerator 1
my $codeGenerator = 0;
open (my $INPUTFILE, "<", $pathToInputFile) or die $!;
while ( my $line = <$INPUTFILE>)
{
if ($line =~ /while \(my \$line = \<\$COMMANDLOG\>\)/)
{
#next line is opening brace
print $line;
$braceLine = <$INPUTFILE>;
print $braceLine;
print "print \"line number in logfile: \$.\\n\"\;" . "\n";
}
elsif ($line =~ /#/)
{
print $line;
}
elsif ($line =~ /(\s)(\$commandApi->Invoke)(.*)/)
{
chomp $line;
my $whiteSpace = $1;
my $invoke = $2;
my $type = $3;
my $printStatement = "print \"";
#replace $ symbol with an escaped \$
#$type =~ s/(\$)/\\$1/;
#replace every instance of a " in the arguments to an escaped \"
$type =~ s/(")/\\$1/g;
#remove closing semicolon
$type =~ s/\;//;
#add closing parenthesis and semicolon with a newline
$type .= "\\n\"\;";
#build the line
$matchedLine = $whiteSpace . $printStatement . $invoke . $type;
#append a newline to appear at the end of hte line
#$matchedLine .= "\\n";
#append a line terminator
#$matchedLine .= "\;";
#append a newline, so that the code is echoed to the terminal correctly
$matchedLine .= "\n";
print "$matchedLine";
}
else
{
print $line;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment