changing format comment char
changing format comment char
am 11.10.2007 23:21:01 von bfc
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
Re: changing format comment char
am 11.10.2007 23:38:35 von Paul Lalli
On Oct 11, 5:21 pm, b...@fenway.UUCP (Time Waster) wrote:
> 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
Re: changing format comment char
am 11.10.2007 23:57:08 von bfc
In article <1192138715.781963.5970@19g2000hsx.googlegroups.com>,
Paul Lalli wrote:
>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.
Re: changing format comment char
am 12.10.2007 01:41:25 von Ben Morrow
Quoth noone@nowhere.com:
> In article <1192138715.781963.5970@19g2000hsx.googlegroups.com>,
> Paul Lalli wrote:
> >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;
> >}
>
> 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 <
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
Re: changing format comment char
am 12.10.2007 01:57:55 von Jim Gibson
In article , Time Waster
wrote:
[suggested program using here document snipped]
> That was my first choice, a quick try didn't make it work. Using
> something closer to yours:
>
> #!/usr/bin/perl
use strict;
use warnings;
> 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.
>
Adding those two lines will tell you what the problem is.
See also 'perldoc -q documents' "Why don't my <
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: changing format comment char
am 12.10.2007 02:51:08 von Big and Blue
Time Waster wrote:
> 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
>.......
>
> ..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?
As has been pointed out, you don't want to do that anyway, but if you ever
come across a case where you do then all you need to do is put the difficult
line into a string and print the string:
==========
...
my $str="#!/usr/bin/expect";
format =
@*
$str
spawn some-commnd
expect -re "some string"
more here of course
..
.....
==========
--
Just because I've written it doesn't mean that
either you or I have to believe it.
Re: changing format comment char
am 12.10.2007 03:23:18 von bfc
Nice!
Thanks everyone, i'm using the simple redirect from inline now.
In article ,
Big and Blue wrote:
>As has been pointed out, you don't want to do that anyway, but if you ever
>come across a case where you do then all you need to do is put the difficult
>line into a string and print the string:
>
>==========
>...
>
>my $str="#!/usr/bin/expect";
>
>format =
>@*
>$str
>spawn some-commnd
>expect -re "some string"
>more here of course
>.
>
>....
Re: changing format comment char
am 12.10.2007 18:34:52 von Glenn Jackman
At 2007-10-11 05:21PM, "Time Waster" wrote:
> 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?
In addition to all the other suggestions, you can use the __DATA__
section of your script. I do this often when I want to include some
code for another language:
#!/usr/bin/perl
my $expect_script = join '', ;
...
__DATA__
#!/usr/bin/exprct
...
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Re: changing format comment char
am 12.10.2007 18:35:11 von glex_no-spam
Time Waster wrote:
> Nice!
>
> Thanks everyone, i'm using the simple redirect from inline now.
Whatever that means.