Tweet

Displaying a Text Message

This page illustrates three ways to display text using perl. As usual, we provide complete working examples for you to copy and paste.

Display a text message on a terminal or console

    #!/usr/bin/perl
    use strict;
    use warnings;

    print "Some text\n";

Display a text message graphically

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Tk;

    my $mw = MainWindow->new;
    $mw->title("Hello World");
    $mw->Label(-text => "Hello World")->pack();
    $mw->Button(-text => "Done", -command => sub {exit})->pack;
    MainLoop;

Display a text message in a web page

Use the CGI module, create a CGI object (in the variable $q in our case), and call CGI methods like header and start_html to output the html.

    #!/usr/bin/perl
    use strict;
    use warnings;
    use CGI;
    my $q = new CGI;
    print $q->header,
          $q->start_html('Your page title'),
          'Some plain text',
          $q->b('Some bold text'),
          $q->end_html;

Or using the CGI module's functional programming style, you could do the same with this code:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use CGI qw/:standard/;
    print header,
          start_html('Your page title'),
          'Some plain text',
          b('Some bold text'),
          end_html;
[Top]