将字符串引用为类变量
我正在尝试保存对类变量中字符串的引用。 我希望通过取消引用来访问该变量。 例如,在例程 getHeaders
中,不要使用:
my $fileContentsRef = $this->getFileContent;
my $fileContentsRef1 = $$fileContentsRef;
$fileContentsRef1 =~ /Spaltenname.*?Datentyp.*?---\n(.*?)\n\n/gsmi;
我想使用:
my $fileContentsRef = $this->getFileContent;
$$fileContentsRef =~ /Spaltenname.*?Datentyp.*?---\n(.*?)\n\n/gsmi;
有关更多详细信息,您应该看到末尾的代码。 我的问题是,当我不使用副本时(即当我不使用 $fileContentsRef1
时),该程序无法工作。我在做什么/做错了什么?是否可以按照我描述的方式达到目标?有人可以给我线索吗?
open FILE, "a1.bad";
$file_contents .= do { local $/; <FILE> };
close FILE;
my $log = auswerter->new(\$file_contents);
#-----------------------------------------------------------------
# Subs
#-----------------------------------------------------------------
# CONSTRUCTOR
sub new
{
my $fileRef = $_[1];
my $self = {};
bless $self;
$self->initialize();
if($fileRef) { $self->{fileRef} = $fileRef; }
return $self;
}
sub initialize
{
#-----------------------------------------------------------------
# Configuration
#-----------------------------------------------------------------
my $this = shift;
}
sub setFile {
my $this = shift;
$this->{file} = shift;
}
sub getFileContent
{
my $this = shift;
return $this->{fileRef};
}
sub getHeaders
{
print "HEADERS...\n";
my $this = shift;
my @headers = ();
my $fileContentsRef = $this->getFileContent;
my $fileContentsRef1 = $$fileContentsRef;
$fileContentsRef1 =~ /Spaltenname.*?Datentyp.*?---\n(.*?)\n\n/gsmi;
@headers = split ("\n", $1 );
foreach (@headers)
{
$_ =~ s/^(.*?)\s.*/$1/;
}
return \@headers;
}
sub getErrList
{
print "ERR LIST...\n";
my $this = shift;
my @errors = ();
my $fileContentsRef = $this->getFileContent;
my $fileContentsRef1 = $$fileContentsRef;
$fileContentsRef1 =~ /Spaltenname.*?(Satz.*)ORA.*?^Tabelle/gsmi;
return \@errors if !$1;
@errors = split ("\n\n", $1 );
foreach (@errors)
{
$_ =~ s/.*Spalte (.*?)\..*/$1/msgi;
}
return \@errors;
}
sub getEntries
{
my $this = shift;
my @entries = ();
my $fileContentsRef = $this->getFileContent;
my $fileContentsRef1 = $$fileContentsRef;
$fileContentsRef1 =~ /.*==\n(.*)/gsmi;
@entries = split ("\n", $1 );
return \@entries;
}
sub sqlldrAnalyze
{
my $this = shift;
my $token = shift;
my $errRef =$this->getErrList();
return "" if $#$errRef < 0 ;
my $headersRef = $this->getHeaders();
my $entriesRef = $this->getEntries();
my $i = 0;
my $str = "";
$str = "<html>";
$str .= "<table rules=\"all\">";
$str .= "<tr>";
foreach ( @$headersRef)
{
$str .= "<th>".$_."</th>";
}
$str .= "</tr>";
foreach ( @$entriesRef)
{
my @errOffset = grep { $headersRef->[$_] =~ $errRef->[$i] }0..$#$headersRef ;
my @entries = split($token, $_);
$str .= "<tr>";
foreach (my $j =0; $j <= $#entries;$j++)
{
$str .= "<td nowrap";
$str .= " style=\"background-color: red\"" if $j == $errOffset[0];;
$str .= ">";
$str .= "<b>" if $j == $errOffset[0];
$str .= $entries[$j];
$str .= "</b>" if $j == $errOffset[0];
$str .= "</td>";
}
$str .= "</tr>\n";
$i++;
}
$str .= "</table>";
$str .= "</html>";
return $str;
}
return 1;
I'm trying to save a reference to a string in a class variable.
I wish to access this variable by dereferencing it.
For example in the routine getHeaders
instead of using:
my $fileContentsRef = $this->getFileContent;
my $fileContentsRef1 = $fileContentsRef;
$fileContentsRef1 =~ /Spaltenname.*?Datentyp.*?---\n(.*?)\n\n/gsmi;
I would like to use:
my $fileContentsRef = $this->getFileContent;
$fileContentsRef =~ /Spaltenname.*?Datentyp.*?---\n(.*?)\n\n/gsmi;
For more details you should see the code at the end.
My problem is, that the program doesn't work when I don't work with the copy( i.e when I don't use $fileContentsRef1
). What am I doing / getting wrong? Is it possible to reach the goal in the way I described? Could some give me clues how?
open FILE, "a1.bad";
$file_contents .= do { local $/; <FILE> };
close FILE;
my $log = auswerter->new(\$file_contents);
#-----------------------------------------------------------------
# Subs
#-----------------------------------------------------------------
# CONSTRUCTOR
sub new
{
my $fileRef = $_[1];
my $self = {};
bless $self;
$self->initialize();
if($fileRef) { $self->{fileRef} = $fileRef; }
return $self;
}
sub initialize
{
#-----------------------------------------------------------------
# Configuration
#-----------------------------------------------------------------
my $this = shift;
}
sub setFile {
my $this = shift;
$this->{file} = shift;
}
sub getFileContent
{
my $this = shift;
return $this->{fileRef};
}
sub getHeaders
{
print "HEADERS...\n";
my $this = shift;
my @headers = ();
my $fileContentsRef = $this->getFileContent;
my $fileContentsRef1 = $fileContentsRef;
$fileContentsRef1 =~ /Spaltenname.*?Datentyp.*?---\n(.*?)\n\n/gsmi;
@headers = split ("\n", $1 );
foreach (@headers)
{
$_ =~ s/^(.*?)\s.*/$1/;
}
return \@headers;
}
sub getErrList
{
print "ERR LIST...\n";
my $this = shift;
my @errors = ();
my $fileContentsRef = $this->getFileContent;
my $fileContentsRef1 = $fileContentsRef;
$fileContentsRef1 =~ /Spaltenname.*?(Satz.*)ORA.*?^Tabelle/gsmi;
return \@errors if !$1;
@errors = split ("\n\n", $1 );
foreach (@errors)
{
$_ =~ s/.*Spalte (.*?)\..*/$1/msgi;
}
return \@errors;
}
sub getEntries
{
my $this = shift;
my @entries = ();
my $fileContentsRef = $this->getFileContent;
my $fileContentsRef1 = $fileContentsRef;
$fileContentsRef1 =~ /.*==\n(.*)/gsmi;
@entries = split ("\n", $1 );
return \@entries;
}
sub sqlldrAnalyze
{
my $this = shift;
my $token = shift;
my $errRef =$this->getErrList();
return "" if $#$errRef < 0 ;
my $headersRef = $this->getHeaders();
my $entriesRef = $this->getEntries();
my $i = 0;
my $str = "";
$str = "<html>";
$str .= "<table rules=\"all\">";
$str .= "<tr>";
foreach ( @$headersRef)
{
$str .= "<th>".$_."</th>";
}
$str .= "</tr>";
foreach ( @$entriesRef)
{
my @errOffset = grep { $headersRef->[$_] =~ $errRef->[$i] }0..$#$headersRef ;
my @entries = split($token, $_);
$str .= "<tr>";
foreach (my $j =0; $j <= $#entries;$j++)
{
$str .= "<td nowrap";
$str .= " style=\"background-color: red\"" if $j == $errOffset[0];;
$str .= ">";
$str .= "<b>" if $j == $errOffset[0];
$str .= $entries[$j];
$str .= "</b>" if $j == $errOffset[0];
$str .= "</td>";
}
$str .= "</tr>\n";
$i++;
}
$str .= "</table>";
$str .= "</html>";
return $str;
}
return 1;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用文件名参数调用
class->new(...)
构造函数时,new
子例程将类名作为第一个参数,并将文件名作为第二个论点。在构造函数中,您只需将
$_[1]
的值(文件名)复制到$self->{FileRef}
中,但该值不是参考。因此,当您访问它时,无需使用双重印记来取消引用该值。
您应该在顶部使用以下两行运行所有代码,这将为您捕获许多错误(包括在字符串不是引用时尝试使用字符串作为引用):
这两行基本上将 Perl 移出了快速单行代码模式,并转变为更适合大型开发的模式(改进的类型安全性、静态变量名称检查等)。
根据更新:如果您的代码在复制字符串时工作正常,但在直接取消引用时工作不正常,听起来您可能遇到了保留最后匹配位置的字符串引用的问题(
g< /code> 标志)。
尝试运行以下命令:
When you call your
class->new(...)
constructor with a filename argument, thenew
subroutine gets the class name as the first argument, and the filename as the second argument.In your constructor, you are simply copying the value of
$_[1]
(the filename) into$self->{FileRef}
, but that value is not a reference.So when you access it, there is no need to use a doubled sigil to dereference the value.
You should run all of your code with the following two lines at the top, which will catch many errors for you (including trying to use strings as references when they are not references):
These two lines basically move Perl out of quick one-liner mode, and into a mode more suitable for large development (improved type safety, static variable name checking, and others).
Per the update: If the code you have is working properly when copying the string, but not when dereferencing it directly, it sounds like you may be running into an issue of the string reference preserving the last match position (the
g
flag).Try running the following: