| 1 | package Perlito5::Runtime::Formline; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Data::Dumper; | ||
| 5 | |||
| 6 | # a pure Perl implementation of CORE::formline() | ||
| 7 | # | ||
| 8 | # TODO - special variables: $: $^ $~ $% $= $- $^L | ||
| 9 | # | ||
| 10 | |||
| 11 | sub formline { | ||
| 12 | my $picture = shift; | ||
| 13 | |||
| 14 | my $supress_line_if_all_fields_empty = 0; | ||
| 15 | my $repeat_line_until_all_fields_exhausted = 0; | ||
| 16 | |||
| 17 | if ($picture =~ /~/) { | ||
| 18 | $supress_line_if_all_fields_empty = 1; | ||
| 19 | if ($picture =~ /~~/) { | ||
| 20 | $repeat_line_until_all_fields_exhausted = 1; | ||
| 21 | } | ||
| 22 | $picture =~ s/~/ /g; | ||
| 23 | } | ||
| 24 | $picture =~ s/[ ]*$//; # trim spaces at the end of line before interpolating | ||
| 25 | my @parts = split | ||
| 26 | /( [\@\^] | ||
| 27 | (?: | ||
| 28 | <+ (?: \.\.\. )? # @<<<< @<<<<... | ||
| 29 | | \|+ (?: \.\.\. )? # @|||| @||||... | ||
| 30 | | >+ (?: \.\.\. )? # @>>>> @>>>>... | ||
| 31 | | 0? \#+ \. \#+ # @###.### @0###.### | ||
| 32 | | 0? \#+ # @### @0### | ||
| 33 | | 0? \. \#+ # @.### @0.### | ||
| 34 | | 0 # @0 | ||
| 35 | | \* # @* | ||
| 36 | | # @ | ||
| 37 | ) | ||
| 38 | )/x, $picture; | ||
| 39 | # print Dumper \@parts; | ||
| 40 | do { | ||
| 41 | if ($supress_line_if_all_fields_empty) { | ||
| 42 | my $empty = 1; | ||
| 43 | for (@_) { | ||
| 44 | $empty = 0 if defined($_) && $_ ne ''; | ||
| 45 | } | ||
| 46 | $^A = "" if !defined($^A); | ||
| 47 | return 1 if $empty; | ||
| 48 | } | ||
| 49 | my $out = ""; | ||
| 50 | my $var_index = 0; # we access the parameter list from @_, because the form parameters are "rw" | ||
| 51 | PART: | ||
| 52 | for my $part_index (0 .. $#parts) { | ||
| 53 | my $s = $parts[$part_index]; | ||
| 54 | if ( substr($s, 0, 1) eq "^" ) { | ||
| 55 | # special field | ||
| 56 | my $regular_field = $s; | ||
| 57 | $regular_field =~ s/\^/\@/; | ||
| 58 | if ($regular_field eq '@*') { | ||
| 59 | $_[$var_index] =~ s/^([^\n*]\n?)//; # modify the parameter | ||
| 60 | my $var = $1; | ||
| 61 | $var_index++; | ||
| 62 | $out .= _format($regular_field, $var); | ||
| 63 | } | ||
| 64 | elsif ($regular_field =~ /\@[\.#0]/) { | ||
| 65 | my $var = $_[$var_index++]; | ||
| 66 | if (defined($var)) { | ||
| 67 | $out .= _format($regular_field, $var); | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | $out .= " " x length($regular_field); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | if (!defined($_[$var_index]) && $part_index == $#parts) { | ||
| 75 | next PART; # skip last field if the variable is undef | ||
| 76 | } | ||
| 77 | my $len = length($regular_field); | ||
| 78 | $_[$var_index] =~ s/^(.{0,$len})//; # modify the parameter | ||
| 79 | my $var = $1; | ||
| 80 | $var_index++; | ||
| 81 | $out .= _format($regular_field, $var); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | elsif ( substr($s, 0, 1) eq "@" ) { | ||
| 85 | # regular field | ||
| 86 | $out .= _format($s, $_[$var_index++]); | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | $out .= $s; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | # print "[[ $out ]]\n"; | ||
| 93 | $^A .= $out; | ||
| 94 | } | ||
| 95 | while $repeat_line_until_all_fields_exhausted; | ||
| 96 | return 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub _format { | ||
| 100 | my ($picture, $value) = @_; | ||
| 101 | # print "_format [[ $picture ]] [[ $value ]]\n"; | ||
| 102 | |||
| 103 | if ($picture eq '@*') { | ||
| 104 | chomp($value); | ||
| 105 | return $value; | ||
| 106 | } | ||
| 107 | |||
| 108 | if ($picture =~ /\@[\.#0]/) { | ||
| 109 | my $fmt = "%"; | ||
| 110 | $fmt .= "0" if $picture =~ /0/; | ||
| 111 | $fmt .= length($picture); | ||
| 112 | my $dot = index( $picture, "." ); | ||
| 113 | if ($dot > 0) { | ||
| 114 | $fmt = $fmt . "." . ( length($picture) - $dot - 1 ); | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | $fmt = $fmt . ".0"; | ||
| 118 | } | ||
| 119 | $fmt .= "f"; | ||
| 120 | # print "_format sprintf [[ $fmt ]]\n"; | ||
| 121 | my $out = sprintf( $fmt, $value ); | ||
| 122 | # print "_format out [[ $out ]]\n"; | ||
| 123 | if (length($out) > length($picture)) { | ||
| 124 | return "#" x length($picture); | ||
| 125 | } | ||
| 126 | return $out; | ||
| 127 | } | ||
| 128 | |||
| 129 | if (length($value) < length($picture)) { | ||
| 130 | while (length($value) < length($picture)) { | ||
| 131 | $value = $value . ' ' if $picture =~ / \@\< | \@\| /x || $picture eq '@'; | ||
| 132 | $value = ' ' . $value if $picture =~ / \@\> | \@\| /x && length($value) < length($picture); | ||
| 133 | } | ||
| 134 | # print "_format smaller [[$value]]\n"; | ||
| 135 | return $value; | ||
| 136 | } | ||
| 137 | |||
| 138 | if (length($value) > length($picture)) { | ||
| 139 | $value = substr($value, 0, length($picture) - 3) . "..." if $picture =~ / \.\.\. /x; | ||
| 140 | $value = substr($value, 0, length($picture)); | ||
| 141 | return $value; | ||
| 142 | } | ||
| 143 | |||
| 144 | return $value; | ||
| 145 | } | ||
| 146 | |||
| 147 | 1; | ||
| 148 | |||
| 149 | __END__ |