エラーリスト(定数リスト)からエラーにアクセスする方法を解明しようとしています。アクセスすると、出力された/作成されるエラーを作成するnew()メソッドがあることを意味します。私の新しいメソッドは、私のリストのコードと分かり合っていません。そのため、以下のようなリストからエラー(ハッシュ)を取得できるように変更する必要があります。 ASC::Builder:Error->new(code => UNABLE_TO_PING_SWITCH_ERROR, switch_ip => $switch_ip, timeout => $timeout);
(これが可能であるならば、私はそれが引数としてコードを取り、それを削除せずに、ではないかもしれないと言われている)。ここ個別のperlモジュールにある名前付き定数のリストからハッシュにアクセスしたい
は、私のファイルは以下の通りです:
Error.pm
package ASC::Builder::Error;
use strict;
use warnings;
use ASC::Builder::Error::Type 'code';
use parent 'Exporter';
our @EXPORT_OK = qw/new/;
sub new {
my ($class, %args) = @_;
my @args = keys %args;
# Takes code in as an argument and then removes it so the error hash it self can be assigned to it
my $self = delete $args{code};
if (is_error($self)) {
return $self;
}
# 1st argument will be error hash. Any other arguments will be context params & inserted into
# context field of the error hash
if (ref $self eq 'HASH' && (@args > 1)) {
foreach my $key (@{ $self->{context} }) {
# And take the ones we need
$self->{args}->{$key} = $args{$key};
}
my @template_args = map { $self->{args}->{$_} } @{ $self->{context} };
# Map/Insert arguments into context hash and insert into string template
$self->{message} = sprintf ($self->{template}, @template_args);
return bless $self, $class;
}
# Supporting the old error messaage (string & parameters)
else {
return bless { message => $args[0]}, $class;
}
}
# Accessor for category
sub category {
return shift->{category};
}
# Accessor for message
sub template {
return shift->{template};
}
# Accessor for context
sub context {
return shift->{context};
}
# Accessor for template option
sub tt {
return shift->{tt}{template};
}
# Accessor for fatal
sub is_fatal {
return shift->{fatal};
}
# Setter for is_fatal
sub set_is_fatal {
my ($self, $fatal) = @_;
$self->{fatal} = $fatal;
}
# Accessor for wiki_page
sub wiki_page {
return shift->{wiki_page};
}
# Accessor for args. args are a hash ref of context parameters that are
# passed in as a list at construction
sub args {
return shift->{args};
}
# Accessor for error message which is processed inside the new() method.
# Will return error message with context parameters inserted.
sub message {
return shift->{message};
}
# Stringifies the error to a log message (for SB dashboard), including the
# category, message, and wiki_page.
sub stringify {
my ($self) = @_;
return sprintf ("%s: %s\nMore info: %s",$self->{category}, $self->{message}, $self->{wiki_page});
}
# Accessor for old error message type
sub details {
my $self = shift;
return $self->{details} || $self->{message};
}
sub code {
return shift->{code};
}
# Used to deserializ from build json.
sub recreate_from_hash {
my($class, $hash) = @_;
return bless $hash, $class;
}
# Use to check if something is out error.
sub is_error {
if (scalar(@_) > 1) { # Called as $class->is_error
shift; # Get rid of class
}
return UNIVERSAL::isa(shift, 'ASC::Builder::Error');
}
1;
Type.pm(これは私が輸出しているリストです)
package ASC::Builder::Error::Type;
use strict;
use warnings;
use parent 'Exporter';
# Export the list of errors
our @EXPORT_OK = ('code');
# List of error messages
use constant code => {
UNABLE_TO_PING_SWITCH_ERROR => {
category => 'Connection Error',
template => "Could not ping switch %s in %s seconds.",
context => [ qw(switch_ip timeout) ],
tt => {template => 'disabled'},
fatal => 1,
wiki_page => 'http://w.error-fix.com/index.php/Builder/ErrorCodes/UNABLE_TO_PING_SWITCH_ERROR',
},
# Add errors to this library
};
1;
私がしたいことができない場合、誰かがその理由を説明してくれますか? :) –
'Type.pm'モジュールが'} 'のためにコンパイルに失敗しました。修正してください。 –
Woo、申し訳ありませんが、エラーのリスト全体を1つだけのエラーが必要なので、質問の目的のために取り出しました。今すぐ修正します:) –