vlturner has asked for the wisdom of the Perl Monks concerning the following question:

I forgot how to place string text in a variable if a function fails.

my $hostname = gethostbyaddr(inet_aton($data[$ip_address_pos]), AF_INET) or "Can't resolve $data[$ip_address_pos]: $!\n";

Update

OK, fixed it... But have new problem. For some reason PR-Z8K-KC35-0001 kills inet_ntoa() Any ideas???

use Data::Validate::Domain qw(is_hostname); if ( $data[$ip_address_pos] eq "" && $data[$name_pos] ne "" && is_hostname($data[$name_pos])) { my $address = inet_ntoa(inet_aton($data[$name_pos])) || "Error: Can't resolve."; splice @data, ($ip_address_pos), 1, $address; }

Debug data

<c> DB<67> p @data truefalse8falsetruefalse0190c812242e8240298da5114dc38b0d1300577https://staplessb.service-now.com/api/now/table/cmn_location/fd7829030a0a3c0e002edd5fb99dd428fd7829030a0a3c0e002edd5fb99dd428falsehttps://staplessb.service-now.com/api/now/table/sys_user_group/globalglobal4USD0false2015-09-18 23:55:34https://staplessb.service-now.com/api/now/table/cmdb_model/88a82faa89ee2100298df3b18393ef3e88a82faa89ee2100298df3b18393ef3efalsefalsetrueManual Entryfalsetruehttps://staplessb.service-now.com/api/now/table/sys_user_group/14341bae0a0a3c9b019f95720822cb7a14341bae0a0a3c9b019f95720822cb7afalse2015-09-18 23:55:03PR-Z8K-KC35-0001-VOICE - LocalVOICE - Localsales office. We are bringing up the circuit 9/111300577PR-Z8K-KC35-0001PRODtrue2015-09-18 23:55:34CI10333615readyu_cmdb_ci_network_circuit2015-09-11 20:13:550 DB<68> p $data$name_pos PR-Z8K-KC35-0001 DB<69> n Use of uninitialized value in subroutine entry at ServiceNowCMDB_CI_FQDN_Dump.pl line 87. at ServiceNowCMDB_CI_FQDN_Dump.pl line 87. Bad arg length for Socket::inet_ntoa, length is 0, should be 4 at ServiceNowCMDB_CI_FQDN_Dump.pl line 87. at ServiceNowCMDB_CI_FQDN_Dump.pl line 87.

  • Comment on Forgot variable syntax for $var = funct() or "Funct failed\n" / Any experience with is_hostname()
  • Select or Download Code

Replies are listed 'Best First'.
Re: Forgot variable syntax for $var = funct() or "Funct failed\n";
by Eily (Monsignor) on Jan 05, 2016 at 15:41 UTC

    or is one of the lowest precedence operators, even below =. This means that your code is parsed as: (my $hostname = call()) or "Can't resolve thing"; You should use || instead, or // if your value can be defined but false.

    my $hostname = call() || "Can't resolve"; my $hostname = call(); $hostname //= "Can't resolve"; # Alternative so +lution

      my $hostname = call(); $hostname //= "Can't resolve"; # Alternative solution

      Or even just
          my $hostname = call() // "Can't resolve";

      c:\@Work\Perl>perl -wMstrict -le "sub S { return; } ;; my $s = S() // 'cannot resolve'; print qq{'$s'}; " 'cannot resolve'


      Give a man a fish:  <%-{-{-{-<

        Yes of course, it was implied in my first proposition. My point was that splitting a complex statement in two was also a valid way to disambiguate and obtain the expected value. I just used || in one case and // in the second for diversity.

Re: Forgot variable syntax for $var = funct() or "Funct failed\n";
by kennethk (Abbot) on Jan 05, 2016 at 15:43 UTC
    A read through Operator Precedence and Associativity in perlop would probably be informative. You have used the low-precedence or instead of the high-precedence ||. Alternatively, you can always use parentheses.
    my $hostname = gethostbyaddr(inet_aton($data[$ip_address_pos]), AF_INET) || "Can't resolve $data[$ip_address_pos]: $!\n"; my $hostname = (gethostbyaddr(inet_aton($data[$ip_address_pos]), AF_INET) or "Can't resolve $data[$ip_address_pos]: $!\n" );

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Forgot variable syntax for $var = funct() or "Funct failed\n";
by AnomalousMonk (Archbishop) on Jan 05, 2016 at 16:34 UTC
    my $hostname = gethostbyaddr(...) or "Can't resolve $data[$ip_address_pos]: $!\n";

    This may not be pertinent, but I notice you use  $! in your error message. However, gethostbyaddr sez:

    For the *gethost*()* functions, if the "h_errno" variable is supported in C, it will be returned to you via $? if the function call fails.
    (Note $? versus $!)

    Update: Actually, I can't seem to get gethostbyaddr to link usefully, so see
        perldoc -f gethostbyaddr
    from your friendly local command line.


    Give a man a fish:  <%-{-{-{-<

      You need to link to endservent. Makes perfect sense, right?
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Forgot variable syntax for $var = funct() or "Funct failed\n";
by Anonymous Monk on Jan 05, 2016 at 15:58 UTC

    The other monks have answered your question; if you want to check precedence issues yourself, B::Deparse can be very useful:

    $ perl -MO=Deparse -e 'my $foo = somefunc() or "text"' '???' unless my $foo = somefunc(); $ perl -MO=Deparse,-p -e 'my $foo = somefunc() or "text"' ((my $foo = somefunc()) or '???'); $ perl -MO=Deparse,-p -e 'my $foo = somefunc() || "text"' (my $foo = (somefunc() || 'text'));
Re: Any experience with is_hostname()
by Anonymous Monk on Jan 05, 2016 at 21:52 UTC