1. Perl
  2. builtin functions
  3. here

print function - prints a string

Use the print function to print the string.

print "Hello World!";

You can also assign a string to a variable and then pass it to the print function. This method is recommended when actually writing a program. It is a method to create one string and then pass it to the print function.

my $message = "Hello World!";
print $message;

You can also pass list or an array to the print function. The strings are concatenated and output. When actually writing a program, the above method is recommended over this method.

# List
print "Hello", "World", "!";

# arrangement
my @strings = ("Hello", "World", "!");
print @strings;

How to output after concatenating an array into one string

A common technique is to concatenate array strings with commas and output with a line break at the end.

This is done using the print function and join function, string concatenation operator You can write beautifully.

my @animals = ('Dog', 'Cat', 'Mouse');
print join(',', @animals) . "\n";

The default output destination of the print function is standard output

The default output destination for the print function is the screen, or standard output . Please refer to the following article for a detailed explanation of standard output.

The default output destination of the print function is standard output, and the following two writing styles have the same meaning.

print "Hello";
print STDOUT "Hello";

Note that there is no comma after STDOUT. This is called "indirect object syntax" and has the same meaning as the following notation.

STDOUT->print("Hello");

It is Perl's convention to use indirect object syntax when specifying the output destination in the print function.

Specify standard error output

You can specify standard error output as the output destination. Specify STDERR as the output destination.

print STDERR "Hello";

Standard error output is for errors. For a detailed explanation of standard error output, refer to the following article.

Specify file handle

You can also change the output destination of the print function to a file. In that case, open the file with open function, create a file handle, and specify the file handle with the print function.

my $file = "output.txt";
open my $fh, '>', $file
  or die "Can't open \" $file\":$!";
print $fh "Hello";

"Hello" is output to a file called "output.txt".

Same as the echo function in the shell

If you're using bash, you'd probably use echo for output, which is a feature of Perl's print function.

If you want to format it, printf function or sprintf function

If you want to specify the format like the C language printf function, use printf function or sprintf function and pass it to the print function.

# printf function
printf "Number%d", 3;

# sprintf function
my $string = sprintf("Number%d", 3);
print $string;

Technique to output to a file by redirect

A common technique is to redirect standard output to a file. This technique allows you to output to a file without using the open function.

First, write the print function normally.

# hello.pl
print "Hello";

Then use redirect to output to a file. Execute the command as follows.

perl hello.pl > output.txt

"Hello" is output to a file called "output.txt".

Related Informatrion