Tweet

Using the Perl print() function

The print function prints a string, or list of strings to your screen, or to a file.

Print one string

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

    print "Hello there\n";

    Hello there

Print two strings

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

    print "Hello there - ", "this is a longer example\n";

    Hello there - this is a longer example

Print also evaluates a calculation

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

    print "Here's a calculation: " . 12 * 3 . "\n";

    Here's a calculation: 36

Print to a file

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

    open FILE, ">output.txt"                 # Create the file "output.txt"
        or die "Could not create file: $!";  # in the current directory
    print FILE "Hello there\n";              # Print a string to the file
    close FILE                               # Close the file
        or die "Could not close file: $!";

The file will contain:

    Hello there

More

To find out more, run the command:

    perldoc -q print
Revision: 1.4 [Top]