#! /usr/bin/perl -w
#
# un_commentLines.pl - Comments or uncomments the selected lines
# Uses '# ' for Perl and shell scripts; '// ' otherwise
my $outputString = "";
my $perlCmt = "#";
my $cCmt = "//";
# get path to document
my $headerPath = <<'HEADERPATH';
%%%{PBXFilePath}%%%
HEADERPATH
chomp $headerPath;my $commentString;
if ($headerPath =~m/\.(sh|pl|py)$/) {
$commentString = $perlCmt;
} else {
$commentString = $cCmt;
}
my @selection =
# no chars in selection, so create an empty selection
if (!@selection) {
push @selection, "";
};
# add or remove comment markers depending on the state of the first line of the selection
# if it is uncommented, comment all lines. If it is commented, remove comment markers, if present
my $firstLineOfSelection = $selection[0]; #get first line
my $addingCommentsString = 1;
if ($firstLineOfSelection =~ /^$commentString/) { #selection starts with comment
$addingCommentsString = 0;
}
foreach my $line (@selection) {
if ($addingCommentsString == 1) {
$outputString .= $commentString.$line;
} else {
$line =~ s/^$commentString//;
$outputString .= $line;
}
}
print "%%%{PBXSelection}%%%";
print $outputString;
print "%%%{PBXSelection}%%%";
I was getting errors using your script as is.
ReplyDeleteI changed the line
my @selection = ; # read the selection from standard input
to
my @selection = ; # read the selection from standard input
so that it would read the selection appropriately and now it seems to be working fine.
Andre, I guess it was a typo, the right line is:
ReplyDeletemy @selection = ; # read the selection from standard input
I get it, it is not possible to post the right line :( I'll explain it: The line is the same, but between the equal and the semicolon there should be written STDIN inclosed in smaller/bigger signs..
ReplyDelete