Learning Perl adds DOS command examples

Windows commands get short shrift when we write about system, and I’m just as guilty as anyone else. In the “Process Management” chapter Learning Perl, we barely mention Windows. In the seventh edition, I want to change that to have a Windows example for each Unix example if I can develop a reasonable homologue. With Stackoverflow, that’s not so hard.

We started the chapter with the “Unix date command”:

system 'date';

This one is almost easy because DOS also has a command named date, but by itself it prompts for a new date. With the /T date it doesn’t prompt:

system 'date /T';

The next one isn’t so bad to translate either. We used the ls command to demonstrate the use of a shell variable that Perl should not interpolate:

system 'ls -l $HOME';

We know that’s harder than using ~ to specify the home directory but that’s not the point of the example. The homologous Windows command does not have the interpolation problem:

system 'dir %userprofile%';

The Stackoverflow answer to What is the alternative for ~ (user’s home directory) on Windows command prompt? has some other options, but this one seems the least complicated.

Putting an application in the background is easy on a Unix shell:

system 'some_command &';

In Windows, there’s the start command. It doesn’t put the process in the background but it separates it so the parent command can continue:

system 'start /B somecommand';

The C<< /B >> suppresses the creation of a new window. There are some issues about which environment the new command gets, but it can go off to do its work while the main program continues. Stackoverflow has an an answer for Powershell, but I’m going to skip Powershell in Learning Perl. I’ll have to explore that in a different post.

Leave a Reply

Your email address will not be published. Required fields are marked *