Perl错误"非法八进制数字' 8'"在URL中阅读时

时间:2014-05-28 18:50:04

标签: perl

我正在尝试格式化.txt文件以适合我的实验室用于上传项目数据的格式。在.txt文件中是我用作博客条目ID的URL。但是,当我将它们作为STDIN传递给我的Perl脚本时,我收到此错误:

Illegal octal digit '8' at Desktop/blog.txt line 3, at end of line

这是它失败的网址:

http://thealbinobean.blogspot.com/2013/08/we-are-all-miley-cyrus.html

我理解错误的发生是因为Perl正在解释" 08"作为八进制数,但我无法找到Perl将整个URL解释为字符串的方法。这是我的代码:

use strict;
use warnings;

my $count = 0;
my $blogName;
my $entryID;

# desired format:
# {{ID=URL}}{{USER=BLOG NAME}}
# Blog entry on one line
# "<ENDOFBLOG>"

foreach my $line (<STDIN>) {
    if($count == 0) {
        $blogName = $line;
    } elsif($line =~ /^http/) {
        $entryID = $line;
        print "{{ID=$entryID}}{{USER=$blogName}}";
    } elsif($line eq "<ENDOFBLOG>") {
        print "<ENDOFBLOG>";
    } elsif($line !~ /^\s*$/) {
        print $line;
    }
    $count++;
}

问题:如何让Perl将输入解释为字符串以避免这种八进制解释?

1 个答案:

答案 0 :(得分:2)

你有没有运行你的程序?我收到了大量的编译错误:

  • 宣布my时,您在foreach循环中没有使用$line
  • 您的初始变量声明没有。[/ li>
  • 您正在使用$_,但未在任何地方进行设置。
  • 您未在print声明中打印任何NL。
  • 您正在使用+来连接字符串。这是添加。您使用.连接

而且,你甚至不必那样做。 Perl连接变量没有问题:

print "{{ID=$entryID}}{{USER=$blogName}}\n";

让您的计划正常运作。修复这些错误。给我们一些可以使用的数据,这样我们就可以弄清楚你的程序正在做什么。


附录

  

我不明白为什么我没有收到这些编译错误。否则,我会在提交之前修复所有这些内容。也许它与我如何执行脚本并在终端中输入它输入有关,因为我得到的唯一错误是上面列出的那个。

这是 原始 计划:

use strict;
use warnings;

my count = 0;
my blogName;
my entryID;

# desired format:
# {{ID=URL}}{{USER=BLOG NAME}}
# Blog entry on one line
# "<ENDOFBLOG>"

foreach $line (<STDIN>) {
    if(count == 0) {
        blogName = $line;
    } elsif($_ =~ /^http:/) {
        entryID = $line;
        print "{{ID=" + entryID + "}}{{USER=" + blogName + "}}";
    } elsif($line eq "<ENDOFBLOG>") {
        print "<ENDOFBLOG>";
    } elsif($_ !~ /^\s*$/) {
        print $line;
    }
    count++;
}

这是我得到的错误:

No such class count at ./test.pl line 5, near "my count"
syntax error at ./test.pl line 5, near "my count ="
No such class blogName at ./test.pl line 6, near "my blogName"
No such class entryID at ./test.pl line 7, near "my entryID"
Global symbol "$line" requires explicit package name at ./test.pl line 14.
Global symbol "$line" requires explicit package name at ./test.pl line 16.
Global symbol "$line" requires explicit package name at ./test.pl line 18.
Global symbol "$line" requires explicit package name at ./test.pl line 20.
Global symbol "$line" requires explicit package name at ./test.pl line 23.
Bareword "count" not allowed while "strict subs" in use at ./test.pl line 15.
Bareword "blogName" not allowed while "strict subs" in use at ./test.pl line 15.
Bareword "entryID" not allowed while "strict subs" in use at ./test.pl line 18.
Bareword "entryID" not allowed while "strict subs" in use at ./test.pl line 19.
Bareword "blogName" not allowed while "strict subs" in use at ./test.pl line 19.
Bareword "count" not allowed while "strict subs" in use at ./test.pl line 25.
Execution of ./test.pl aborted due to compilation errors.