1

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

11 Comments

Can't modify constant item in scalar assignment at jdoodle.pl line 7, near ""/var/home/smoketest/dhcpv6.pl.28709.log";" Execution of jdoodle.pl aborted due to compilation errors.
That's because I typed the code without testing it and had syntax errors ;) The variables were missing $ symbols. Should be fixed now
I tried this : sub potatoex { my $potato="Junos: 17.4DCB"; if($potato = ~/"Junos: 17.4[a-zA-Z0-9_.]*"/) { return "/var/home/smoketest/dhcpv6.pl.28709.log"; } } main : { my $ex= potatoex(); my $goodge= "Apurva $ex Arnav"; print $goodge; }
@ApurvaArnav yw, just remember to upvote, select answers, and contribute on SO ;)
I'm sorry, but this is the first time I am using SO, how to upvote and select answers?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.