2017-09-16 19 views
1

我試圖用它的字符串值(它從子程序「potatoex()」中取出變量「ex」並將它放在另一個變量「距離Goodge」, 沒有得到任何語法錯誤,但是這似乎不工作了 請幫將變量替換爲字符串與perl中的子例程的結果

sub potatoex {  
    my $potato="Junos: 17.4DCB"; 
    if($potato = ~/"Junos: 17.4[a-zA-Z0-9_.]*"/) { 
    print "/var/home/smoketest/dhcpv6.pl.28709.log"; 
    } 
} 

主:。

{ 
    my $ex= potatoex(); 
    my $goodge= "Apurva $ex Arnav"; 
    print $goodge; 
} 

CURRENT O/P:在/ var /家庭/ smoketest/dhcpv6.pl.28709.logApurva 1 Arnav

預期O/P:Apurva /var/home/smoketest/dhcpv6.pl.28709.log Arnav

感謝, Apurva

回答

1

您是print從子程序而不是return荷蘭國際集團荷蘭國際集團的價值從中。嘗試在potatoex中使用return

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

雖然你可以在if塊以上(而不是print)使用return,我轉而決定使用一個變量,它總是會返回要麼你正試圖將值或空字符串。您可以明確聲明返回return returnVal;,但它不是必需的,因爲perl允許隱式返回(請記下這一點並確定它是否適合您的最佳實踐)。

+0

謝謝,但我得到這個錯誤: –

+0

不能修改jdoodle.pl第7行標量賦值中的常量項,「」/var/home/smoketest/dhcpv6.pl.28709.log「;」 執行jdoodle.pl由於編譯錯誤而中止。 –

+0

這是因爲我輸入了代碼而沒有測試它,並且出現語法錯誤;)變量缺少'$'符號。現在應該修復 – vol7ron