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

Hi I am new to Perl and am using Padre IDE. How do I align the text in a print statement

print "How do I align this statement so that it does not look like a r +un on sentence and take up the whole screen? \n";

Replies are listed 'Best First'.
Re: text alignment
by haukex (Archbishop) on Apr 21, 2017 at 17:26 UTC
    How do I align the text in a print statement

    I assume you mean wrap, not align. It depends on where you want the wrapping to happen:

    • Just visually, in the editor? Try turning on "line wrap"/"word wrap" (don't have a copy of Padre handy here so I can't tell you exactly which menu it's under)
    • In the source file? There are several techniques, the simplest is probably using the . (dot) concatenation operator:

      print "How do I align this statement so " ."that it does not look like a run on " ."sentence and take up the whole screen? \n";

      There are also modules to help you with formatting your Perl source, in particular perltidy, although that won't break up long strings like that for you.

    • On output? Store the text in a variable and then use a module like the core module Text::Wrap (I see 1nickt has already pointed this out).
Re: text alignment
by 1nickt (Canon) on Apr 21, 2017 at 17:17 UTC

    See the core module Text::Wrap.


    The way forward always starts with a minimal test.
Re: text alignment
by kcott (Archbishop) on Apr 22, 2017 at 08:33 UTC

    If you're talking about output, maybe consider using a heredoc. That's described in "perldoc: Quote-Like Operators". Here's an example:

    #!/usr/bin/env perl use strict; use warnings; print <<'EOT'; How do I align this statement so that it does not look like a run on sentence and take up the whole screen? EOT

    Output:

    How do I align this statement so that it does not look like a run on sentence and take up the whole screen?

    You may also find formatted printing, with printf, useful. See the sprintf documentation for the formatting codes (and a huge number of examples).

    — Ken