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);

我收到错误:不支持协议“c”

我得到正确的输出时

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

因为我想让它在所有系统上运行,所以我正在使用全局环境变量。

如何使用正则表达式将 $url 中的 '\' 更改为 '/' 或者有其他方法吗?

HTML 文件存在于系统本身的本地。

4

2 回答 2

3

使用Path::Class::URI创建跨平台file://URI。

于 2013-09-19T16:30:49.423 回答
2

这应该适合你。

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);
于 2013-09-19T16:30:32.043 回答