changing format comment char

Discussion in 'Perl Misc' started by Time Waster, Oct 11, 2007.

  1. Time Waster

    Time Waster Guest

    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
     
    Time Waster, Oct 11, 2007
    #1
    1. Advertising

  2. Time Waster

    Paul Lalli Guest

    On Oct 11, 5:21 pm, (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
     
    Paul Lalli, Oct 11, 2007
    #2
    1. Advertising

  3. Time Waster

    Time Waster Guest

    In article <>,
    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.
     
    Time Waster, Oct 11, 2007
    #3
  4. Time Waster

    Ben Morrow Guest

    Quoth :
    > In article <>,
    > 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 <<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
     
    Ben Morrow, Oct 12, 2007
    #4
  5. Time Waster

    Time Waster Guest

    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:
    >
    >==========
    ><start of code>...
    >
    >my $str="#!/usr/bin/expect";
    >
    >format =
    >@*
    >$str
    >spawn some-commnd
    >expect -re "some string"
    >more here of course
    >.
    >
    >....<rest of code>
     
    Time Waster, Oct 12, 2007
    #5
  6. Time Waster

    J. Gleixner Guest

    Time Waster wrote:
    > Nice!
    >
    > Thanks everyone, i'm using the simple redirect from inline now.


    Whatever that means.
     
    J. Gleixner, Oct 12, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alec S.
    Replies:
    10
    Views:
    10,269
    Alec S.
    Apr 16, 2005
  2. wwj
    Replies:
    7
    Views:
    590
  3. wwj
    Replies:
    24
    Views:
    2,562
    Mike Wahler
    Nov 7, 2003
  4. Ben Pfaff
    Replies:
    5
    Views:
    496
    Tristan Miller
    Jan 17, 2004
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,130
    Ian Collins
    May 9, 2006
Loading...

Share This Page