Replace a variable as string with results from a subroutine in perl - 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).

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 })
}

Access an array item in Perl after generating that array

Hello I want to access an specific array item based in a condition previously checked. I leave the code here:
elsif (scalar(#{$boss->bosses}) > 1) {
foreach my $pa (#{$boss->bosses}) {
my $p = My::Model::Group->new(id => $pa->group_id);
push(#$groups, $p);
$valid_pass = 1 if ($pa->checkPassword($self->param('password')));
}
if ($valid_pass) {
my $pa_id = $pa->id;
my $pa_partner_id = $pa->group_id;
}
else {
}
}
What I want to do is, if that if in the array that comes, I check if the password is correct, so if it's correct, then I want to take the id and the group_id of the array item to use it in a function to be able to log them in.
Your for loop is doing two things at once: producing a list of My::Model::Group objects in #$groups, and finding the first boss whose password checks out.
I suggest that you split them up into two clear operations, and the List::Util modules first operator is ideal for the second task
Here's how it would look. I've extracted the result of the method call $boss->bosses into a variable $bosses to avoid repeated calls to the method
Note that you don't need to apply scalar to an array when checking its size. The > and all the other comparators impose scalar context anyway
I've taken much of my code from your question, and I'm a little concerned that you extract values for $pa_id and $pa_partner_id and then just discard them. But I imagine that you know what you really want to do here
use List::Util 'first';
my $bosses = $boss->bosses;
if ( ... ) {
...;
}
elsif ( #$bosses > 1 ) {
#$groups = map { My::Model::Group->new( id => $_->group_id ) } #$bosses;
my $password = $self->param( 'password' );
my $pa = first { $_->checkPassword( $password ) } #$bosses;
if ( $pa ) {
my $pa_id = $pa->id;
my $pa_partner_id = $pa->group_id;
}
else {
...;
}
}

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/;
}
}

htmlspecialchars() expects parameter 1 to be string, array given in for zend

I have an error in file : Abstract.php
Warning: htmlspecialchars() expects parameter 1 to be string,array given in /usr/local/zend/share/ZendFramework/library/Zend/View/Abstract.php on line 905
I have this on line 905 :
public function escape($var)
{
if (in_array($this->_escape, array('htmlspecialchars', 'htmlentities'))) {
return call_user_func($this->_escape, $var, ENT_COMPAT, $this->_encoding);
}
if (1 == func_num_args()) {
return call_user_func($this->_escape, $var);
}
$args = func_get_args();
return call_user_func_array($this->_escape, $args);
}
i don't understand this...
Probably you added array to $this->escape() function.
If you don''t know where then add to file Abstract.php something like this:
if(is_array($var)){
Zend_Debug::dump($var);//this will print variable causing problem
//OR
throw new Exception(' ');//this will print debug backtrace showing which line caused problem
}
I was having same issue on lib/Varien/Data/Form/Element/Abstract.php
I did update below
protected function _escape($string)
{
return htmlspecialchars($string, ENT_COMPAT);
}
with
protected function _escape($string)
{
if(is_string($string)) {
return htmlspecialchars($string, ENT_COMPAT);
}
else
{
return $string;
}
}
Thing started to work.

How can I return a whole hash map in Perl?

I have a hash called
%values
Now I want to return the whole hash in a subroutine
sub getvalues {
return $values;
}
But then I got an error, because $value needs a definition and my program stops. If I'm using
sub getvalues {
return %values;
}
it seems to work, but my program is very slow and don't get further... So how can I return the whole map?
It would be nice to return the hash reference instead of hash,what you need to do is
First stote the hash into the hash ref then return it like
sub getvalues {
my %values = (test => "SO");
my $values = \%values;
return $values;
}