priyanka priyanka - 7 months ago 18
Perl Question

How can I convert my shell script to Perl?

I have a shell script:

#!usr/bin/bash
branch_name=$1
task_name=$2
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.

!/usr/bin/perl
use warnings;
use strict;
my branch_name;
my task_name;
createviewset ccm -b $branch_name -t $task_name
source setenv $task_name


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

Answer

You should visit http://perlmaven.com/ (available in many languages) or http://learn.perl.org/ 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:

#!/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:

#!/usr/bin/perl
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.

#!/usr/bin/perl
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.

Comments