mikejones has asked for the wisdom of the Perl Monks concerning the following question:

When using format, instead of having a @< defined as a 100 character field, is there a way to say @< x 100 or anyway other way instead of having to type @< 100 times ?
format STDOUT_TOP = @<<<<<<<<<<< qx(hostname) Page @<<< $% <snip> . format STDOUT = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<< @<<<<<< @<<<<<<<<<<<< <<<<<<<<<<< @|||||||||||||||||| @##.## <snip> ~~ .

Replies are listed 'Best First'.
Re: format characters
by Fletch (Bishop) on Feb 06, 2008 at 21:43 UTC

    You could always build the format dynamically and then eval it</slightly-evil> :)

    I also think Perl6::Form has support for specifying a width numerically rather than by literal picture, but I'm not recalling how to do it offhand.

    Update: Aaah, search for "Imperative field width".

    use strict; use Perl6::Form; my( $name, $age ) = ( "Fred", 23 ); print join( " "x9, 0..7 ), "\n", "0123456789"x7, "012\n"; print form "{<{50}<}|{|||} ", $name, $age; exit 0;

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      OK, you already said it was evil, but eval'ing formats is accident-prone, as you will have to escape all the '@' signs. I'm with shmem here: use the editor functionality.


      Enjoy, Have FUN! H.Merijn
Re: format characters
by shmem (Chancellor) on Feb 06, 2008 at 23:51 UTC
    You can build your format template using formline and the accumulator $^A (see perlform and perlvar). But if it's just a typing laziness issue, get a decent editor. E.g. with vi, the sequence
    o@<^[x50p

    (^[ being the escape key) will result in a @ followed by fifty < chars:

    @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      I am trying what you suggested (and thank you!) but its not working. I want to replace this code:
      <snipped the _TOP> format STDOUT = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<< @<<<<<<< @<<<<<<<<<<<<< <<<<<<<<<< @||||||||||||||||||| @##.## $sfile, + $user, $grp, $gcos, dateme($mod,$mod2), $sz ~~ .

      with this code

      my $format = "format STDOUT = \n" . '@' . '<' x 125 . $sfile . q{ } . '@' . '<' x 8 . $user . q{ } . '@' . '<' x 8 . $grp . q{ } . '@' . '<' x 25 . $gcos . q{ } . '@' . '|' x 20 . dateme($mod,$mod2) . q{ } . '@' . '#' x 2 . '.##' . $sz . "\n" . "~~\n" . ".\n" ;

      And the errors I am getting are:

      Use of uninitialized value in concatenation (.) or string at find_hog. +tdy line 49. Use of uninitialized value in concatenation (.) or string at find_hog. +tdy line 49. Use of uninitialized value in concatenation (.) or string at find_hog. +tdy line 49. Use of uninitialized value in concatenation (.) or string at find_hog. +tdy line 49. Use of uninitialized value in localtime at find_hog.tdy line 105. Use of uninitialized value in concatenation (.) or string at find_hog. +tdy line 49. format STDOUT = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<< +@<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<< @||||||||||||||||||||19:00:00- +12/31/69 @##.## ~~ . Undefined format "STDOUT" called at find_hog.tdy line 227.

      Do I use a regular write? I am confused on how write knows about $format?

        I am trying what you suggested (and thank you!) but its not working.

        Huh? I didn't suggest that - I wrote about an editor (vi) command. But since you are bringing that up - re-read perlform, the chapter "Accessing Formatting Internals".

        If you construct your format into a string, you have to either eval it -

        my $format = '| @' . '>' x 10 . ' | ' . '@' . '<' x 10 . " |\n" . '$given, $surname' ."\n" . '.'; eval "format STDOUT = \n$format"; for ([qw(Bill Bruford)],[qw(Ginger Baker)]) { ($given, $surname) = @$_; write; } __END__ | Bill | Bruford | | Ginger | Baker |

        or use formline and the accumulator $^A :

        my $format = '| @' . '>' x 10 . ' | ' . '@' . '<' x 10 . " |\n"; for ([qw(Bill Bruford)],[qw(Ginger Baker)]) { formline( $format,@$_); print $^A; $^A = ''; } __END__ | Bill | Bruford | | Ginger | Baker |

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: format characters
by apl (Monsignor) on Feb 06, 2008 at 21:32 UTC
    To the best of my knowledge, you are out of luck. (That is, I have to hit '<' or '>' just as often as you do. Unfortunately.)

    I look forward to being corrected.