Perl tips

Perl

– Variable Interpolation

This is a classic between many programming languages.

When we use a double quoted string, for every variable that we put inside the string , we get variable’s value as part of the output when we execute our script.

For example :

 #!/usr/bin/perl
#script1 use 5.012;
my $name = "Stavros";
print "My name is $name\n";

script1 output –>  My name is Stavros

 

On the other hand , interpolation doesn’t happen with single-quoted strings, for example :

 

 #!/usr/bin/perl
#script2 use 5.012;
my $name = "Nick";
print 'My name is $name\n';

script2 output –> My name is $name\n

Here both the variable name and the newline character are printed in the screen, so they are not interpolated by Perl.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.