Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6391333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:39:32+00:00 2026-05-25T03:39:32+00:00

I wrote a script, where i slurp in UTF-8 encoded HTML-file and then parse

  • 0

I wrote a script, where i slurp in UTF-8 encoded HTML-file and then parse it to tree with HTML::Tree. Problem is that after parsing the strings are not marked as UTF-8 anymore.

As _utf8_on() is not recommended way to set flag on, i am looking for proper way.

My simplified code-example:

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;
use utf8::all;
use autodie;
use HTML::Tree;
use Encode qw/is_utf8/;

my $file = shift;
my $tree;

if ($file) {
    my $content = slurp_in( 'file' => $file );
    $tree = html_tree('content' => $content);
} else {
    die "no file";
}

my $title = $tree->look_down(_tag => 'title');
$title = $title->as_HTML('');

if ( is_utf8( $title ) ) {
    say "OK: $title";
} else {
    say "NOT OK: $title";
}

## SUBS
##
sub slurp_in {
    my %v = @_;

    open(my $fh, "<:utf8", $v{file}) || die "no $v{file}: $!";
    local $/;
    my $content = (<$fh>);
    close $fh;

    if ($content) {
        return $content;
    } else {
        die "no content in $v{file} !";
    }
}

sub html_tree {
    my %v = @_;
    my $tree = HTML::Tree->new();
    $tree->utf8_mode(1); ## wrong call here, no such method, but no warnings on it!
    $tree->parse( $v{content} );

    if ($tree) {
        return $tree;
    } else {
        die "no tree here";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:39:33+00:00Added an answer on May 25, 2026 at 3:39 am

    Your code is overcomplicated, and you employ utf8::all and decode manually and call that strange method all at once. Rhetorically asking, what do you expect to achieve that way? I do not have the patience to find out the details what goes wrong and where, especially since you did not post any input with which your program fails to do the expected, so I drastically reduce it to a much simpler one. This works:

    #!/usr/bin/env perl
    use 5.010;
    use strict;
    use warnings FATAL => ':all';
    use File::Slurp qw(read_file);  # autodies on error
    use HTML::Tree qw();
    
    my $file = shift;
    die 'no file' unless $file;
    
    my $tree = HTML::Tree->new_from_content(
        read_file($file, binmode => ':encoding(UTF-8)')
    );
    
    my $title = $tree->look_down(_tag => 'title');
    $title->as_HTML(''); # returns a Perl string
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a script in python that scans a file and extract strings from
I wrote a script that performs some adjustments, saves then closes the image: preferences.rulerUnits
I wrote a script that collects all our log files to one tar file.
I wrote a script that reads each file in a directory, does something and
I wrote this script which counts occurrences of particular pattern in a given file.
I wrote a script that uses xcodebuild to generate an AdHoc build of an
I've got strange problem with cut I wrote script, there I have row: ...
I wrote a script for screen pops from our soft phone that locates a
I wrote this script to my live search: $(document).ready(function(){ $('input#search').keypress(function(){ $('ul#pacientes_hint').html(); var texto =
I wrote one script which gets an image url through JSONP, and then I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.