G'day morgon,

"... how can I turn it off so I can send headers with leading double-underscores?"

The short answer first. Somewhere near the start of your code you'll have:

use HTTP::Request;

Change that line to:

BEGIN { use HTTP::Request; $HTTP::Headers::TRANSLATE_UNDERSCORE = 0; }

That's the fix. Read on for details ...

My test:

#!/usr/bin/env perl -l use strict; use warnings; BEGIN { use HTTP::Request; $HTTP::Headers::TRANSLATE_UNDERSCORE = 0; } use Data::Dump; my $r = HTTP::Request->new; my $header = $r->header('__X_Y__', 1); dd $r;

With just "use HTTP::Request;", instead of that BEGIN block, I get:

bless({ _content => "", _headers => bless({ "--x-y--" => 1, "::std_case" => { "--x-y--" => " +--X-Y--" } }, "HTTP::Headers"), _method => undef, _uri => undef, }, "HTTP::Request")

With the BEGIN block, as shown, I get:

bless({ _content => "", _headers => bless({ "::std_case" => { __x_y__ => "__X_Y__" }, "__x_y +__" => 1 }, "HTTP::Headers"), _method => undef, _uri => undef, }, "HTTP::Request")
"Why is that ..."

A little investigation, using the published documentation, provided the answer.

In HTTP::Request, under "$r->header( $field => $value )", you'll see:

"This is used to get/set header values and it is inherited from HTTP::Headers ..."

In HTTP::Headers, under "$h->header( $field => $value )", you'll see:

"To make the life easier for perl users who wants to avoid quoting before the => operator, you can use '_' as a replacement for '-' in header names."

You'll also see similar comments for other methods.

Following the Source link (at the top of the page), shows:

... # The $TRANSLATE_UNDERSCORE variable controls whether '_' can be used # as a replacement for '-' in header field names. our $TRANSLATE_UNDERSCORE = 1 unless defined $TRANSLATE_UNDERSCORE; ... $field =~ tr/_/-/ if $TRANSLATE_UNDERSCORE; ...

I searched the documentation of both HTTP::Request and HTTP::Headers for "TRANSLATE_UNDERSCORE": no matches were found. Perhaps you'd care to raise a bug report for a documentation enhancement.

— Ken


In reply to Re: double underscore in http-header by kcott
in thread double underscore in http-header by morgon

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.