Re: How can we interpolate an expression??
by Kenosis (Priest) on Sep 20, 2012 at 18:11 UTC
|
use Modern::Perl;
my $number = 5;
my $multiplier = 3;
say "Product is @{[$number * $multiplier]}";
Output:
Product is 15
| [reply] [d/l] [select] |
|
|
What I understand from this code is: -
First we are converting the expression into an array or list..
Now, had we printed out that list or array, it would have printed out its address.. So, we have added an '@' before it to get an array.. Now, because we are not using a reference variable to this list, we have used a curly braces before using '@'
Have I interpreted it correct.. Is this what happens in Perl??
Or can I get a better explanation of what is happening there?
| [reply] |
|
|
say "Product is @{[$number * $multiplier]}";
^ ^ ^ ^
| | | |
| | | + - EXPRESSION
| | + - reference
| + - dereference
+ - interpolate
| [reply] [d/l] |
|
|
|
|
|
Re: How can we interpolate an expression??
by toolic (Bishop) on Sep 20, 2012 at 18:04 UTC
|
use warnings;
use strict;
use 5.14.0;
my $number = 5;
my $multiplier = 3;
say "Product is $number * $multiplier";
say "Product is ", $number * $multiplier;
__END__
Product is 5 * 3
Product is 15
| [reply] [d/l] |
|
|
Can't we make your first "say ..." statement to print like the second one??
When we do this: -
say "number is $number";
It simply replaces the $number with its value.. Shouldn't it do the same for an expression.. or we have to do it in some different way? | [reply] [d/l] |
|
|
If the interpreter implemented the interpolation you are specifying, then how could I as a programmer make it output the string Product is 5 * 3? There'd be some pretty non-intuitive side effects.Kenosis below has given you a way to be clever, but this is all getting a little clever for my taste. For complex output, I usually prefer going all the way to printf since, after all, you desire a formatted print. So maybe printf "number is %d\n", $number * $multiplier;. toolic's solution still strikes me as pretty clean as well.
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
| [reply] [d/l] [select] |
|
|
Re: How can we interpolate an expression??
by Anonymous Monk on Sep 20, 2012 at 17:55 UTC
|
| [reply] |
|
|
print "The square of $answer is ", $answer * $answer, "\n";
print takes a list :) this list can be of a single item. join takes a list and makes it a single string
print join '', "The square of $answer is ", $answer * $answer, "\n";
you can also use the join operator (concatenation)
print "The square of $answer is ". $answer * $answer. "\n";
If you insist on interpolation, see
- http://perldoc.perl.org/perltrap.html#Interpolation-Traps
- @ always interpolates an array in double-quotish strings
- Arbitrary expressions are evaluated inside braces within double quotes
| [reply] [d/l] [select] |
|
|
Yes.. I can use single quotes and '.' to concatenate the results just as we do in Java..
say 'Result is :' . $num * $multipler;
| [reply] [d/l] |
|
|
-
my $arrayref = [ 4200 / 100 ];
print "The answer is $arrayref \n";
- The answer is ARRAY(0x3f8a94)
-
my $arrayref = [ 4200 / 100 ];
print "The answer is @$arrayref \n";
- The answer is 42
-
my $arrayref = ;
print "The answer is @[ 4200 / 100 ] \n";
-
syntax error at - line 1, near "= ;"
Execution of - aborted due to compilation errors.
-
print "The answer is @{[ 4200 / 100 ]}\n";
- The answer is 42
| [reply] [d/l] [select] |
Re: How can we interpolate an expression??
by ww (Archbishop) on Sep 20, 2012 at 18:51 UTC
|
... as a one-liner; nothing else here is new, other than escaping the variables to avoid interpolation.
C:\>perl -E "my $num1=3; my $num2=4; say \"The result of \$num1 * \$nu
+m2 is: \" . $num1*$num2;"
The result of $num1 * $num2 is: 12
| [reply] [d/l] |
Re: How can we interpolate an expression??
by Marshall (Canon) on Sep 21, 2012 at 07:43 UTC
|
my $number = 5;
my $multiplier = 3;
# Use print instead of "say"
print "Product is ", $number * $multiplier, "\n";
__END__
Product is 15
"say" is a short hand way to add the "\n" at the end of the line.
when you don't mean or need that, just old fashioned print will work great. | [reply] [d/l] |
|
|
Well, the only one different that I know between print and say is that: - -->"say" adds a Newline to the output, whereas we have to give it explicitly in "print".
Is there any other different??
| [reply] |
|
|
my $number = 5;
my $multiplier = 3;
# Use print instead of "say"
print "Product is ", $number * $multiplier, "\n";
#this does not work...
#"say" has limitations...
say "Product is ", $number * $multiplier;
__END__
Product is 15
"Say" will add a \n to a simple string.
More complex things require "print" or "printf". | [reply] [d/l] |
Re: How can we interpolate an expression??
by Rohit Jain (Sexton) on Sep 20, 2012 at 18:25 UTC
|
Yeah.. got it worked with that set of "Curly" braces and "Square" brackets around the expression..
Thanks everyone :) | [reply] |