Print the return of a method
I know in php I can do something like this
echo "{$this->method}";
and I swear there was a way to do it in perl
Update: What I am trying to do is print a s开发者_开发知识库calar that the method returns. I was kind of hoping of doing within the string like in php, just because I'm lazy :P.
Are you just trying to evaluate an arbitrary expression inside a double quoted string? Then maybe you're thinking of
print "@{[$this->method]}";
There is also a trick to call the method in scalar context, but the syntax is a little less clean.
print "${\($this->method)}";
Well, if $this->method
outputs a string or a number (like PHP, Perl can automatically convert numbers to strings when required), then you can do print $this->method . "\n";
.
If $this->method
outputs a data structure (eg an array reference or a hash reference), you can use Data::Dumper
to look at the structure of the data. Basically, print Dumper($foo)
is the Perl equivalent of PHP's var_dump($foo)
.
What are you trying to do, exactly?
If $this->method
is returning a string, you can do this:
print $this->method . "\n";
without quotes. That will print your string. Sometimes, that can lead to a clumsy looking statement:
print "And we have " . $this->method . " and " . $that->method . " and " . $there->method . "\n";
In that case you can use a little programming trick of:
print "And we have @{[$this->method]} and @{[that->method]} and @{[$their->method]}\n";
Surrounding a function with @{[]}
prints out the function's value. Someone explained this to me once, but I can't remember why it works.
精彩评论