Wednesday, May 16, 2007

Of coding Perl in relation to Ksh scripting

I know that there are many differences between shell scripting and Perl, I am reminded of that daily while I constantly switch between the two. Although today, I ran across a difference that I wanted to share with everyone.

In scripting, you can do something like the following:

##### begin code #####

myVar = this

myNextVar = ${myVar}andthat

print $myNextVar

##### End Code #####

The output of this would be: thisandthat

The point above is that you can enclose the variable(except for the dollar sign) in curly braces and it is interpolated to its value and then whatever comes after the curly braces will be appended to the name.

Well, this becomes impossible to do as shown above as the curly braces are used for other things in Perl. So, I did some thinking and figured that I could use the concatenatin character to solve my problem. Here is the above code as how I would have written it in Perl:

##### Begin Perl Code #####

my $var = "this";
my $nextVar = "$var" . "andthat";

print $nextVar;

##### End Perl Code #####

The output of the above Perl code is exactly the same as the shell script, but as you can see, we have to put things together in a slightly different way.

Happy Perl coding!

No comments:

 
Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.