1
use HTML::TreeBuilder::XPath;

my $temp_path =  $ENV{'TEMP'}."\\html\\globals_func.html";   

// prints as C:\Users\Rockstar\Appdata\Local\Temp\html\globals_func.html

my $url = $temp_path;
my $page = get($url) or die $!;
my $p = HTML::TreeBuilder::XPath->new_from_content($page);

I get error: protocol 'c' is not supported

I get correct output when

my $url='file:///C:/Users/Rockstar/AppData/Local/Temp/html/globals_func.html';

As I want to make it run across all systems I am using global environment variable.

how do I change '\' to '/' in $url by using regex or is there any other way?

HTML file is present locally in the system itself.

3
  • 1
    There's no need to use LWP for slurping a local file. Use File::Slurp or do it manually using open($fh,...) and <$fh>, then you can use the path as is. Commented Sep 19, 2013 at 19:04
  • okay. will look into that too. Commented Sep 20, 2013 at 17:11
  • i used File::Slurp and its so simple without using so much of regex .just use read_line($file_path) and now my program also runs on linux."file:///" was causing trouble Commented Sep 20, 2013 at 20:04

2 Answers 2

3

Use Path::Class::URI to create cross-platform file:// URIs.

Sign up to request clarification or add additional context in comments.

Comments

2

This should work for you.

use HTML::TreeBuilder::XPath;

my $temp_path =  $ENV{'TEMP'}."\\html\\globals_func.html";   

// prints as C:\Users\Rockstar\Appdata\Local\Temp\html\globals_func.html

$temp_path=~tr/\\/\//; # Replaces backward slashes with forward slashes

$temp_path='file://'.$temp_path; # Appends path with file://

my $url = $temp_path;
my $page = get($url) or die $!;
my $p = HTML::TreeBuilder::XPath->new_from_content($page);

1 Comment

it works fine. but you missed a '/'. it should be $temp_path='file:///'.$temp_path;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.