I don't know of any module that provides this, but it doesn't seem like it would be that hard to implement.

Oh what the heck, Something like this perhaps? (Could probably use some further testing.)

UPDATE: fixed missing \a (bell) handling.

use warnings; use strict; use Test; my ($linecount, $current_col, $tab) = (0, 0, 4); sub pr { for my $string (@_){ print $string; my $lines += () = $string =~ /[\n\r]/g; $current_col = 0 if $lines; $linecount += $lines; $string =~ s/.*[\n\r](.*)$/$1/sg; $string =~ s/\t/' ' x $tab/eg; my $bs = $string =~ tr/\b//; my $bell = $string =~ tr/\a//; $current_col += (length $string) - $bs * 2 - $bell; $current_col = 0 if $current_col < 0; $linecount += int($current_col / 80); $current_col %= 80; } } BEGIN { plan tests => 24 }; my @tests = ( ["1234567890", 0, 10], ["12345\n67890", 1, 5], ["12345\n67890\n12345", 2, 5], ["12345\r67890", 1, 5], ["12345\r67890\r12345", 2, 5], ["12345\t67890", 0, 10 + $tab], ["1234567890\b\b\b\b\b", 0, 5], [qw/1234567890 1234567890 1234567890 1234567890 1234567890/, 0, 50], ["1234567890" x 10, 1, 20], ["1234567890" x 100, 12, 40], ["1234567890" x 100, "\b" x 50, 12, 0], ["1\b2\b3\b4\b5\b6\b7\b8\b9\b0\b\n12345\t67890\n\t12345\b\b\b\b\b67890 +\t", 2, 5 + $tab*2], ["1234567890\a\b\a\b\a\b", 0, 7], ); for my $test (@tests){ ( $linecount, $current_col ) = ( 0, 0 ); my @strings = @$test; splice @strings, -2, 2, ''; pr $_ for @strings; print "<--\n"; ok($linecount, $test->[-2], "Linecount incorrect"); ok($current_col, $test->[-1], "Column count incorrect"); }

In reply to Re: scalar or sub that can tell me the terminal print offset ? by thundergnat
in thread scalar or sub that can tell me the terminal print offset ? by palkia

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.