php run method and get variable in one line - class

I want to get variable after running class function. I usually do it in 2 lines:
$object->function();
$var = $object->function_variable;
Can I do it somehow in one line?
$var = $object->function()->function_variable;
UPDATE
In general I want to call for different vars from one function in different files
$var = $object->function()->function_variable1;
$var = $object->function()->function_variable2;
$var = $object->function()->function_variable3;

Why doesn't function() easily return your function_variable?

You need to learn about the basics of oop,
If the function returns the value that you want to assign to $var, then you can do:
$var = $object->function();

I suppose you want to access instance variable not function variable. So it will be like:
$var = $object->function()->instance_variable;
For this to work your function should return an object with those instance variables, then only you will be able to achieve this.
For more clarity see the below example:
<?php
class Name
{
public $fname, $lname;
function __construct($fname, $lname)
{
$this->fname = $fname;
$this->lname = $lname;
}
}
class Person
{
private $name;
public function __construct($fname, $lname)
{
$this->setName($fname, $lname);
}
public function getName()
{
return $this->name;
}
private function setName($fname, $lname)
{
$this->name = new Name($fname, $lname);
}
}
$p = new Person('Raj', 'Mishra');
$name = $p->getName();
print $var = $name->fname; // $var = $object->instance_variable1;
print " ";
print $var = $p->getName()->lname; // $var = $object->function()->instance_variable2;
?>

Related

Perl optimization: inlining a function changed the results

I profiled some code, and there is one function which stood out and I was wondering if there is a way to optimize it:
The function is defined as:
sub convert_arrayref {
if(ref($_[0]) eq 'ARRAY') {
return join(($_[1] || ","), #{$_[0]});
}
else {
return $_[0];
}
}
Most of the times the else block will get executed, and I was wondering if I could inline it rather than making a function call. The calling code looks as follows:
$data = convert_arrayref($data, '&')
So, what I did was change the calling code as:
if ($data eq 'ARRAY') {
$data = join('&', $data)
}
I thought this would be equivalent. However, the results are different. I am wondering if I have done something wrong here.
You get different results because you did not replicate the functionality of the if clause. You need to use ref to check if your variable is an array reference, and you need to deference the variable:
if (ref($data) eq 'ARRAY') {
$data = join('&', #{ $data })
}

Replace a variable as string with results from a subroutine in perl

I am trying to replace the variable "ex" with it's string value(which it gets from subroutine "potatoex()" and put it in the definition of another variable "goodge",
not getting any syntax error but this seems to be not working out.
Please help.
sub potatoex {
my $potato="Junos: 17.4DCB";
if($potato = ~/"Junos: 17.4[a-zA-Z0-9_.]*"/) {
print "/var/home/smoketest/dhcpv6.pl.28709.log";
}
}
main :
{
my $ex= potatoex();
my $goodge= "Apurva $ex Arnav";
print $goodge;
}
CURRENT O/P : /var/home/smoketest/dhcpv6.pl.28709.logApurva 1 Arnav
EXPECTED O/P: Apurva /var/home/smoketest/dhcpv6.pl.28709.log Arnav
Thanks,
Apurva
You are printing from your subroutine instead of returning the value from it. Try using return in potatoex.
sub potatoex {
my $potato = q{Junos: 17.4DCB};
my $returnVal = '';
if( $potato =~ /Junos: 17\.4[a-zA-Z0-9_.]*/ ) {
$returnVal = q{/var/home/smoketest/dhcpv6.pl.28709.log};
}
$returnVal;
}
{
my $ex = potatoex();
my $goodge = qq{Apurva $ex Arnav};
print $goodge;
}
While you could use return in an if block above (instead of print), I instead decided to use a variable, which will always return either the value you are trying to or an empty string. You could explicitly state the return return returnVal;, but it isn't required as perl allows implicit returns (make note of this and figure out if it should fit in your best practices or not).

SugarQuery build up queryOr in a for loop

I am trying to build up a queryOr based on a comma separated list that is passed in by the user that I will loop over and add the values.
So far I got this which just builds it:
$query = new SugarQuery();
$query->from(BeanFactory::getBean('rdwrk_Request'));
$skills = $query->join('rdwrk_request_skills_rdwrk_request', array('alias' => 'skills'))->joinName();
$query->select(array(array('name', 'requestName'), array('skills.name', 'skillName')));
if($args["requestName"])
{
$requestName = $args["requestName"];
if(strpos($requestName, ',') !== false)
{
$requestNames = explode(",", $requestName);
foreach($requestNames as $name)
{
$query->where()->queryOr()->contains('name', $name);
}
}
else
{
$query->where()->contains('name', $requestName);
}
}
if($args["skillName"])
{
$query->where()->contains('skills.name', args["skillName"]);
}
$results = $query->execute();
Is there any way to build it so all the values I loop over go into the same queryOr?
You can set the current SugarQuery object into its own variable, and then feed it through your loop as follows:
$currentQuery = $query->where()->queryOr();
foreach($requestNames as $name)
{
$currentQuery->contains('name', $name);
}
This calls the contains function on the same query object with the queryOr established. Since the $currentQuery variable references the same SugarQuery object, running $query->execute() later on will include the parameters.

Parsing text File and extracting information in perl

I have a code with multiple functions
void fun_one()
{
tempa=10;
tempb=10;
}
void fun_two()
{
tempc=12;
tempd=13;
}
I want perl code that extracts function name if i input variable name;
say I search for "tempa", it should extract "fun_one" into a variable;
say I search for "tempd", it should extract "fun_two" into a variable;
I have extracted line number
open my $code, '<', 'code.txt' ;
my $keyword = "tempa";
my $regex = qr|\b($keyword)\b|;
while ($code>)
{
while (/$regex/g)
{
print "$.\n";
}
}
Output:3
I need some logic to extract fuction name.
thanks in advance
You might need a parser instead of just regexes.
But...
sub find_function_using_variable {
my $variable = shift;
my $last_func="";
while (<>) {
$last_func = $1 if m/void (\w+)\(\)/;
print $last_func."\n" if m/\Q$variable\E/;
}
}

Zend Framework, echo message if (!$row) not working

This should be straight forward if the row count is 0 I want to echo a message. Here is what I have:
public function getClubComment($id) {
$id = (int) $id;
$row = $this->fetchRow('club_id = ' . $id);
if (!$row) {
echo 'No comments';
}
return $row->toArray();
var_dump($row);
}
maybe try something like:
//not sure if this will work as I don't do this kind of request anymore
public function getClubComment($id) {
$id = (int) $id;
$row = $this->fetchRow('club_id = ?', $id);
if (!$row) {echo 'No comments';}
return $row->toArray();
var_dump($row);
}
I think you'll be happier doing something like this, takes most of the guess work out.
public function getClubComment($id) {
$id = (int) $id;
//create instance of Zend_Db_Select object
$select = $this select();
$select->where('club_id = ?', $id);
//fetchRow using Zend_Db_Select object
$row = $this->fetchRow($select);
//fetchRow() returns NULL so may as well test for that.
if ($row === NULL) {
throw new Zend_Db_Table_Exception();
}
return $row->toArray();
var_dump($row);
}
Zend_Db_Select is really useful to use in the models as it normally takes care of properly quoting values and it very easy to build a fairly complex sql query with any sql. In this example I used discrete lines for each part of the select() but I could as easily have strung them all together. I personally like each line separate so I can change or troubleshoot easily.
fetchRow returns an object, even if there is no results, that is why the condition in the if statement is always false, try this
if (!count($row))