changing format comment char

T

Time Waster

I wanted to use the format statement to create a script for a
different language:

if (open(FP,">$SOMEFNAME")) {
format FP =
#!/usr/bin/expect
spawn some-commnd
expect -re "some string"
more here of course
..
write FP;
close FP;
}

...but that doesn't work because the '#' means comment, and I can't
seem to escape it. I will continue reading about this feature,
but anyone know offhand if there's a way around this?

TIA
 
P

Paul Lalli

I wanted to use the format statement to create a script for a
different language:

if (open(FP,">$SOMEFNAME")) {
format FP =
#!/usr/bin/expect
spawn some-commnd
expect -re "some string"
more here of course
.
write FP;
close FP;
}

..but that doesn't work because the '#' means comment, and I can't
seem to escape it. I will continue reading about this feature,
but anyone know offhand if there's a way around this?

TIA

Natively, I would try a backslash.

But why on earth are you using formats for this, as opposed to just a
normal string? Or even heredocs?

if (open my $FP, '>', $SOMEFILENAME) {
print $FP << EO_SCRIPT;
#!/usr/bin/expect
spawn some-commnd
expect -re "some string"
more here of course
EO_SCRIPT
close $FP;
}

Paul Lalli
 
T

Time Waster

But why on earth are you using formats for this, as opposed to just a
normal string? Or even heredocs?

if (open my $FP, '>', $SOMEFILENAME) {
print $FP << EO_SCRIPT;
#!/usr/bin/expect
spawn some-commnd
expect -re "some string"
more here of course
EO_SCRIPT
close $FP;
}

Paul Lalli

That was my first choice, a quick try didn't make it work. Using
something closer to yours:

#!/usr/bin/perl
if (open my $FP, '>', "/tmp/tmp.out") {
print $FP << EO_SCRIPT;
#!/bin/sometest
something more
EO_SCRIPT
close $FP;
}

Output:
syntax error at /tmp/tmp2.pl line 7, near "EO_SCRIPT
close"
Execution of /tmp/tmp2.pl aborted due to compilation errors.
 
B

Ben Morrow

Quoth (e-mail address removed):
That was my first choice, a quick try didn't make it work. Using
something closer to yours:

#!/usr/bin/perl
if (open my $FP, '>', "/tmp/tmp.out") {
print $FP << EO_SCRIPT;

Heredocs must be written as <<FOO, not << FOO. What you have there
parses as an attempt to left-shift $FP by the numeric value of
'EO_SCRIPT': if you had used strict it would have forbidden the bareword
EO_SCRIPT, and if you had used warnings it would have warned about the
numeric conversion. This is why you should *always* use both.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top