2 Answers
2

The particular ">:THING" syntax tells the Perl IO system to use the layer specified by THING. Have a look at the PerlIO documentation for 'layer'. Common layers are 'raw' and 'utf8'.

In this case, this allows you to use $stdout as an in-memory file which should end up containing whatever gets sent to STDOUT. More generally, the syntax lets you open an in-memory file, then send the filehandle to other functions that normally write to files, so that you can collect their output (or provide their input).

You can also achieve the same result by opening a "file" which is a reference to a scalar:

Perl uses a layered IO system. At the bottom of the chain, one finds a layer that deals with accessing the media. scalar is the IO system layer that handles reading from and writing to a scalar instead of a file. Saying

open(my $fh, '>:scalar', \$scalar)

for scalar handles is the equivalent of saying

open(my $fh, '>:unix', $file_name)

for OS handles. It's wholly redundant, since Perl already knows it's a scalar handle and not an OS handle.