priyanka priyanka - 2 years ago 70
Perl Question

How can I convert my shell script to Perl?

I have a shell script:

createviewset ccm -b $branch_name -t $task_name
source setenv $task_name
rest of the code

Now I want to convert this script to a Perl script. How can I do that? So far what I have written in the code but this code does not seem to work.

use warnings;
use strict;
my branch_name;
my task_name;
createviewset ccm -b $branch_name -t $task_name
source setenv $task_name

Here the
is an existing script which I am calling here.

Answer Source

You should visit (available in many languages) or to learn some Perl first.

Your shell script doesn't need to copy the commandline values. You also used #!usr/bin/bash which won't work because the path is either /usr/bin/bash or (more common) /bin/bash:

createviewset ccm -b $1 -t $2
source setenv $2
rest of the code

Perl assigns all command line arguments to the array @ARGV. This sample prints your two arguments:

print $ARGV[0];
print $ARGV[1];

Notice that the numbering starts with 0 instead of 1 as in $1 in your bash script.

Next part is running external (shell) commands in Perl: Use the system command.

use strict;
use warnings;

system 'createviewset','ccm','-b',$ARGV[0],'-t',$ARGV[1];
system 'source','setenv',$ARGV[1];

Notice that the source command won't work because a Perl script is not a shell script and can't "include" Bash script. I appreciate that you're trying to use Perl for your problem, but it looks like Bash is the much better tool for this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download