On 25/02/2012 00:41, Sean Murphy wrote:
Hi All.
I have a real issue with strings. I want to build a sub routine
skeleton plus some test code. If I use () or {} etc. The string comes
out completely messed up. The code below is for a heredoc and complains
that I am trying to define a function. If I use the skeleton within a
string. It places the () at() at the beginning. In other words it messes
up the formatting.
Both code examples below:
foreach my $k (keys %commands ) {
my $s;
$k =~ s/show //;
$s = $k;
$s =~ s/ |\-/_/g;
print "<<TEXT";
$s () if(\$k =~ m/$k/);
sub $s () {
} # end sub
String example:
foreach my $k (keys %commands ) {
my $s;
$k =~ s/show //;
$s = $k;
$s =~ s/ |\-/_/g;
print ""$s () if(\$k =~ m/$k/);\n";
print "sub $s () {\} # end sub\n\n";
} # end foreach
It has just occurred to me what you may mean by
It places the () at() at the beginning.
You second version works fine for me (except that you start your string
with two double quotes when you should have only one) but if your $s is
empty (or undefined and you have wanrings disabled) then you will get
() if($k =~ m//);
sub () {} # end sub
so check to make sure that $s holds what you think it does. That is the
only way I can think of to get "() at the beginning".
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/