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

Dear Monks, i tried adding a TXT record via Net::DNS to a testdomain but having problems with escaping the semicolon.
my $txtdata = "v=DKIM1; k=rsa; t=y;p=MIGfMA0GCSqGSIb3DQAFGQUAA4GNADCBi +QKBgQDwWV9npEVjm10XoR1NIx174X/SR+BW8yd4RaXMk+pSDwNx0bFK8fPCXf7tLtFbjC +mYldyhsrnrcloWPVlJkAy41xaTGUcXWpC2Onr/ScbkXa9cuREv4Fy4YmMpHoBKSHoE2JI +8wBEe4oOoHPojT/b+HTNoZmxfkrYqk5eN5jnUewIDAQAB"; my $a = ";"; my $b = "\\" . ";"; # trying to escape the ; $txtdata =~ s/$a/$b/g; my $update = Net::DNS::Update->new("domain.com"); $update->push("update", rr_add("porgy._domainkey.domain.com 600 TXT '$ +txtdata'")); $update->sign_tsig("keyname", "keysign"); my $res = Net::DNS::Resolver->new; $res->nameservers("ns.domain2.com"); my $reply = $res->send($update);
If i do a
host -a porgy._domainkey.domain.com ns.domain2.com
I get the TXT record back, but the semicolon is more than once escaped as:
"v=DKIM1\\\; k=rsa\\\; t=y\\\;p=MIGfMA0GC..."
Anyone is seeing an error on my side ? Thanks

Replies are listed 'Best First'.
Re: Create a TXT record for DKIM using Net::DNS
by ww (Archbishop) on Feb 10, 2009 at 01:13 UTC

    Assuming you want the output to singly escape the semi-colons, thusly:

    v=DKIM1\; k=rsa\; t=y\;p=MIGfMA0GCSqGSIb3DQAFG....

    you don't need to interpolate anything other than the escape (slash). Try using single quotes in the assignments

    my $a = ';';
    my $b = "\\" . ';';

    "A petty consistency..." would suggest single-quoting a single slash, too, but run with ActiveState (w32), strict and warnings, that fails.

    Can't find string terminator "'" anywhere before EOF at 742623.pl line 8.

    If you escape the slash (eg: <c>my $b = '\\' . ';';, Perl will not complain.

      '\' doesn't work because there has to be some way to escape a single quote in a single-quoted literal, which is done - as usual - using a backslash.

      BTW, I wouldn't know why using single quotes here should make a difference with respect to what the OP wants to achieve....

      (As to the OP's problem, my suspicion is that Net::DNS is doing some escaping on its own — though I haven't verified it.)

        I assume too, that Net::DNS is making "some" escaping itself, although http://www.net-dns.org/docs/Net/DNS/RR/TXT.html states: For some applications of the TXT RR the semicolon is relevant, you will need to escape it on input. If i don't do escaping on $txtdata, i get a FORMERR back from bind.