On Jul 24, 12:18 pm, [EMAIL PROTECTED] (Joseph L. Casale)
wrote:
> I have two questions, is it correct to execute subs like this:
> Sub_1 (sub_2))? I am trying to pass the return value of sub_2 into sub_1.
Sure. Just be aware that when calling the "inner" subroutine like
that, you're calling it in list context. More than likely, that won't
make a whole heck of a lot of difference. But if you're dealing with
a subroutine that does something different in list vs scalar context,
you need to be aware of what you're doing.
In other words:
sub_1(sub2());
is equivalent to:
my @values = sub2();
sub1(@values);
rather than
my $value = sub2();
sub1($value);
For example, let's say you were trying to pass your subroutine a
reversed string. A naïve approach would be:
sub1(reverse("foobar"));
You might be very surprised to learn that sub1() is being passed the
string "foobar", not "raboof", because reverse() is being called in a
list context, and so therefore reversed the one-element list
("foobar"), rather than the string "foobar".
> Also, I have been looking at perldoc etc to limit the # of digits returned
> after a
> decimal, it looks difficult? Am I overlooking a simple method?
Yes, and you're not reading perldoc too well. :-P
$ perldoc -q round
Found in /opt2/Perl5_8_4/lib/perl5/5.8.4/pod/perlfaq4.pod
Does Perl have a round() function? What about ceil() and
floor()? Trig functions?
Remember that int() merely truncates toward 0. For rounding
to a certain number of digits, sprintf() or printf() is
usually the easiest route.
printf("%.3f", 3.1415926535); # prints 3.142
Paul Lalli
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/