Convert \x3c/div\x3e to <div> using perl - perl

I have a string which has characters like \x3c/div\x3e , i am trying to convert this to <div> is there any module which helps to solve this issue. I have checked use HTML::Entities but couldn't solve the issue, i am in need of some suggestion.

One method:
my $str = '\x3c/div\x3e';
$str =~ s{\\x([[:xdigit:]]{2})}{chr hex $1}eg;
print $str;
Outputs
<div>

Related

How to do I convert an escaped t into a tab character

I have a variable that contains a slash and a t.
my $var = "\\t";
I want to convert that to a tab. How do I do that?
use Data::Dumper;
use Term::ReadLine;
my $rl = Term::ReadLine->new();
my $var = $rl->readline( 'Enter \t:' );
print Dumper $var;
The following is the simplest solution:
$var = "\t" if $var eq "\\t";
If you want to do this no matter where the sequence appears in the string, you could use
$var =~ s/\\t/\t/g;
But it sounds like you're not asking the right question. Nothing supports \t and nothing else. At the very least, I would also expect \\ to produce \. Are you perhaps trying to parse JSON? If so, there are number of other escape sequences you need to worry about.

Escape angle brackets using Pod::Markdown

I have problem with getting the correct behavior from Pod::Markdown when using brackets < and >. For example:
use strict;
use warnings;
use Data::Dump;
use Pod::Markdown;
my $str = "=head1 OPTIONS\n\n=over 4\n\n=item B<< --file=<filename> >>\n\nFile name \n\n=back\n";
my $parser = Pod::Markdown->new;
my $markdown;
$parser->output_string( \$markdown );
$parser->parse_string_document($str);
dd $markdown;
Gives output:
"# OPTIONS\n\n- **--file=<filename>**\n\n File name \n"
Which gives
on GitHub. So the part <filename> inside the ** tag is probably treated as a HTML tag and therefore not shown.
The desired output would be
"# OPTIONS\n\n- **--file=\<filename\>**\n\n File name \n"
where the brackets < and > should be escaped with a backslash.
Update
Seems like the problem is not restricted to double star sequences. I updated the question according to this..
For the moment, a workaround seems to be to insert a backslash in a postprocessing step. For example:
$parser->output_string( \$markdown );
$parser->parse_string_document($str);
fix_escape_chars(\$markdown);
sub fix_escape_chars {
my ($str) = #_;
$$str =~ s/(?<!\\)>/\\>/g;
$$str =~ s/(?<!\\)</\\</g;
}
This seems to work well.. (It works even inside URLs contrary to what is claimed in this question )..
Pod::Markdown 3.000 has been released and fixes this issue.
Not all markdown processors recognize backslash escaped < chars, so I followed the Markdown spec suggestion of escaping & and < as html entities (& and <).

How can I generate and send an email in HTML format using data from a text file?

I have a file that looks like this:
cat output_title.txt
C817491287 Cat: Nor Sus: something date: 02/26/14
C858151287 Cat: Nor Sus: really something date: 02/26/14
I would like to send an email in HTML format, using parameters from the file, e.g.
mine :firstparamter starting with C
sus: ?
date: ?
How can I do this?
EDIT: CODE
open (FILE, 'output_title.txt');
while (<FILE>) {
chomp;
($chg, $Cat, $category, $sta, $stus, $sus, $open, $open_date) = split(" ");
print "Chnge is:$chg\n";
}
After doncoyote comments :
use strict;
use warnings;
open (FILE, 'output_title.txt');
while (<FILE>) {
my ($Cnum,$Cat,$Sus,$Date) = m!(C\d{9})\s+Cat:\s+(\w+)\s+Sus:\s([\w\s]*?)date:\s+([\d/]+)$! ;
print "Cnum:$Cnum\t";
print "Caty:$Cat\t";
print "Stus:$Sus\t";
print "opendate:$Date\n";
}
close (FILE); exit;
You may find a regex pattern capture to define the required variables works better than split, when there are slight but quantifiable differences in the extraction text.
something like this should handle the cases provided. This could be improved but makes an ok starting point of the top of my head.
my ( $Cnum, $Cat, $Sus, $Date )
= m!(C\d{9})\s+Cat:\s+(\w+)\s+Sus:\s([\w\s]*?)date:\s+([\d/]+)$!
You should start to look into regexes in the perlretut documentation to understand what is going on. Basically the escaped letters w,d,s stand for word digit and non-printable character(spaces,tabs) respectively. The Parentheses capture the pattern and pass those as a list to the assignment variables. The square brackets define a multiple choice of characters.
Quantifiers: + is one or more, * is zero or more, and curly braces is the comma separated specified min/max. Each of the character they immediately follow. The question mark is a non-greedy * and the $ is the end of line anchor.
I'm pretty sure there are several methods to send html-mails from perl.
For example:
use MIME::Lite;
my $msg = MIME::Lite->new(
From => from_you#somedomain.com,
To => to_someone_else#someotherdomain.com,
Subject => "your mail subject",
Type => 'text/html',
Data => qq {
<body>
<table>
<tr> <td>$chg</td><td>$Cat</td>.....</tr>
</table>
</body>
},
);
$msg->send();

use perl to extract a substring between two delimiters and store as a new string

I am working on a Perl script, and I want to split a string between two different variables.
This is my string
<p>Hello my server number is 1221.899999 , please select an option</p>
I want to be able to extract the server number, so I want to split the string after <p>Hello my server number is and before the following space, so my end string would print as
1221.899999
Is regex the best solution for this, rather than using split?
I would just use a regex.
my $str = 'Hello my server number is 1221.899999 , please select an option';
my ($num) = $str =~ /Hello my server number is (\d+\.\d+) ,/;
$num will be undefined if the match didn't succeed.
How about:
$str = 'Hello my server number is 1221.899999 , please select an option';
$str =~ s/^.*\b(\d+\.\d+)\b.*$/$1/;
say $str;
or
$str =~ s/^Hello my server number is (\d+\.\d+)\s.*$/$1/;
If the begining of the string is always that.
output:
1221.899999
I would use regex. How about this:
$str = 'Hello my server number is 1221.899999 , please select an option';
print $1 if $str =~ /is (.*) ,/;
As long as you are sure that there is always a space before the comma, the proper answer is something similar to this
my $string = '<p>Hello my server number is 1221.899999 , please select an option</p>';
my ($server) = $string =~ /server number is (\S+)/;
print $server;
output
1221.899999
If the comma could appear immediately after the end of the server number then you would need to modifiy is slightly to this
my ($server) = $string =~ /server number is ([^\s,]+)/;

Decimal to hex conversion in Perl (– to –)

I have an HTML file with many numerical entities. I want to change them them from using decimal numbers to hex numbers. For example: I want – (en dash) changed to to –.
How does one do this using Perl?
I have tried using the following code:
use String::HexConvert ':all';
my $text = "this is text–example";
print ascii_to_hex($text);
It will converting the all characters. I want to convert the – only.
This will do the trick:
$html =~ s/&#([0-9]+);/ sprintf("&#x%x;", $1) /eg;
(\d matches too many characters.)
Try this:
$text =~ s/\&#(\d+);/"&#x".sprintf("%x",$1).";"/eg
To convert from hex to decimal:
$html =~ s/&#(x[0-9A-Fa-f]+);/ sprintf("&#%d;", hex($1)) /eg;