1

How do you send commands to the terminal in a perl script?

For example, if I want to run the command mkdir $directory in the console, what do I type in the perl script?

1

4 Answers 4

5

For executing any system command from perl script you need use system command with a backtick

For e.g

In your case for creating a directory you need to type

system(mkdir $directory); #note html is not showing the backtick but backtick is place before mkdir and after $directory

It will create a dir in current working dir in the name of $directory

Sign up to request clarification or add additional context in comments.

Comments

4

Modeling off the sample code in the documentation at http://perldoc.perl.org/functions/system.html:

@args = ("mkdir", $directory);
system(@args) == 0
    or die "mkdir failed";

Perl can make directories without system() calls, but I assume you want to do other things and are just using mkdir as an example.

2 Comments

Can also use backticks `` around the command to capture the output, if you need it (eg $i=cat foo.txt puts the contents of foo.txt into $i)
@Nialscorva: Or $i = qx{cat foo.txt} if you need to get around Markdown.
4

Since Perl has the mkdir() system call built in, you don't need to use the system command at all:

my $directory = "...";
if (mkdir $directory)
{
    croak "Failed to create directory $directory ($!)";
}

If you must use system, do so the safe way:

if (system "mkdir", $directory)
{
    croak "Failed to create directory $directory";
}

This form of the system statement (with a list of arguments) avoids the problems of shell expansion.

1 Comment

There's also the File::Path package, which will make or remove more than one level at a time.
0

Use perl commands instead of relying on shell commands. There are a great many things that perl can do, and if it is not already built in, or a core module, it's likely available on CPAN.

As people have mentioned, mkdir is available, and so are most of the basic file related commands.

That being said, here's the dirt on shell commands in perl:

You want the perl script to:

  • Wait while the shell command is performed and check its exit status. Use system
  • Wait and capture the standard output of the shell command. Use qx//, or backticks.
  • Abandon perl script to run another command. Use exec. But usually, you'll not really want to use exec, for reasons described in the link above.
  • Run the system command and pipe input or output to/from it. Use open with MODE either -| or |-, respectively.

2 Comments

It should be stressed that exec does not return to the calling program, i.e. the perl process running the perl code is replaced by the exec'ed program. This means that code following the call to exec is never executed, unless exec fails to invoke the program.
@danielkullmann Good point. I've never really used exec, beyond learning never to use it, some 10-15 years ago. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.