Hello, I noticed that one of my Perl scripts was generating truncated error messages when running on Windows. After some investigations, I found the problem was related to the way the Socket::getaddrinfo function returns the error message (maybe the same thing happens with Socket::getnameinfo).

Here is a code snippet to illustrate the problem:

require Socket; # socktype is set to -1 to make getaddrinfo return an error my ($gaiErr)=Socket::getaddrinfo('perl.org',80,{socktype => -1}); my ($osErr,$extErr)=($!,$^E); print "\$gaiErr=\"$gaiErr\" (".($gaiErr+0).")\n"; print "\$osErr=\"$osErr\" (".($osErr+0).")\n"; print "\$extErr=\"$extErr\" (".($extErr+0).")\n";

Here is what it returns when I run it on a Windows 10 21H1 19043.1586 system (configured in French), with Strawberry Perl 5.32.1-64bit:

$gaiErr="La prise en charge du type de socket spécifié n" (10044) $osErr="Illegal byte sequence" (42) $extErr="La prise en charge du type de socket spécifié n’existe pas da +ns cette famille d’adresses" (10044)

The error message contained in $gaiErr is truncated. It looks like at some point the getaddrinfo function tried to parse/process the error message returned by the corresponding Win32 API call, but it stopped when it encountered the "typographic apostrophe" / "right single quotation mark" character. I guess it didn't use the correct encoding (afaik it should use the Windows ANSI code page of the system). Note that in this case the $^E variable contains the entire correct error message...

For reference, here is the result when I run the same code on a Linux system:

$gaiErr="ai_socktype non supporté" (-7) $osErr="No such file or directory" (2) $extErr="No such file or directory" (2)

This is the result I expected on Windows: the $gaiErr variable contains the entire error message, no need to check $! or $^E (which contains unrelated information in this case).

Maybe it is possible to workaround the problem by adding a line like this one (just after the getaddrinfo call):

$gaiErr=$^E=$gaiErr if($gaiErr && $^O eq 'MSWin32');

But I'm not sure it would always work (it would require all getaddrinfo error code values to be "compatible" with $^E). And anyway it seems a bit ugly... Did I miss something here? Also, is there a way to force usage of unlocalized error messages on Windows from a Perl script?

Thanks !


In reply to Socket::getaddrinfo and localized error messages on Windows by Yaribz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.