如何在Perl中动态别名子例程并传递参数?

huangapple go评论81阅读模式
英文:

Perl how do I dynamically alias a sub and pass it arguments?

问题

在Perl中,您可以动态别名子例程并将参数传递给它,或者将一个子例程别名为另一个子例程在一个blessed对象中。以下是您提供的示例代码中的相关部分的翻译:

  1. #!/usr/bin/env perl
  2. #https://www.perl.com/article/closures-as-objects/
  3. use warnings;
  4. use strict;
  5. use diagnostics;
  6. package Debug;
  7. use Data::Dumper;
  8. sub new {
  9. my $self = {};
  10. # 如何将所有@files别名为子例程log?
  11. my @files = (qw(callback log printing));
  12. foreach my $file (@files) {
  13. # 使用ikegami提出的答案更新
  14. $self->{$file} = sub { plog( @_, $file ) };
  15. }
  16. print Dumper $self;
  17. return bless $self;
  18. }
  19. sub plog { # 提供Debug->log Debug->printing Debug->status等功能
  20. my ($self, $level, $message, $file) = @_;
  21. print "Level:'$level'\n";
  22. print "Message:'$message'\n";
  23. print "File:'$file'\n";
  24. }
  25. 1;
  26. ###################################################
  27. package main;
  28. my $debug = Debug->new();
  29. # 文件 等级,消息
  30. $debug->plog(1, "Hello World", "在Debug中" );
  31. $debug->callback(1, "Hello World" );
  32. $debug->log(1, "Hello World" );
  33. $debug->printing(1, "Hello World" );
  34. #### 期望结果
  35. # Level:1
  36. # Message:Hello World
  37. # File:callback

注意:您提供的Perl代码包含一些HTML转义字符(如>"),这些字符在翻译中已被修复。如果您需要将翻译后的代码放入Perl环境中运行,建议删除这些HTML转义字符。

英文:

In Perl how do I dynamically alias a sub and pass it arguments?
Or How do I alias a sub to another sub in a blessed object?
I have an array @files that I would like to all become subs that call sub log with the name of the file appended to @_
Sample code with things I tried below.
Is this possible? Thanks!

  1. #!/usr/bin/env perl
  2. #https://www.perl.com/article/closures-as-objects/
  3. use warnings;
  4. use strict;
  5. use diagnostics;
  6. package Debug;
  7. use Data::Dumper;
  8. sub new {
  9. my $self = {};
  10. # How do I alias all @files to sub log ?
  11. my @files = (qw(callback log printing ));
  12. foreach my $file (@files){
  13. # updated with ikegami's proposed answer
  14. $self->{$file} = sub { plog( @_, $file ) };
  15. }
  16. print Dumper $self;
  17. return bless $self;
  18. }
  19. sub plog { # provides Debug->log Debug->printing Debug->status etc..
  20. my ($self, $level, $message, $file) = @_;
  21. print "Level:'$level'\n";
  22. print "Message:'$message'\n";
  23. print "File:'$file'\n";
  24. }
  25. 1;
  26. ###################################################
  27. package main;
  28. my $debug = Debug->new();
  29. # file level, message
  30. $debug->plog(1,"Hello World","were in the Debug" );
  31. $debug->callback(1,"Hello World" );
  32. $debug->log(1,"Hello World" );
  33. $debug->printing(1,"Hello World" );
  34. #### Desired result
  35. # Level:1
  36. # Message:Hello World
  37. #File:callback

答案1

得分: 2

Perl对象没有自己的方法;它们拥有其类的方法。

  1. package Debug;
  2. use Sub::Name qw( subname );
  3. for my $name (qw( callback log printing )) {
  4. my $sub = subname $name, sub {
  5. my $self = shift;
  6. return $self->plog( @_ , $name );
  7. };
  8. no strict qw( refs );
  9. *$name = $sub;
  10. }
  11. ...

或者

  1. package Debug;
  2. sub callback { my $self = shift; $self->plog( @_, "callback" ) }
  3. sub log { my $self = shift; $self->plog( @_, "log" ) }
  4. sub printing { my $self = shift; $self->plog( @_, "printing" ) }
  5. ...
英文:

Perl objects don't have the methods of their own; they have the methods of their class.

  1. package Debug;
  2. use Sub::Name qw( subname );
  3. for my $name (qw( callback log printing )) {
  4. my $sub = subname $name, sub {
  5. my $self = shift;
  6. return $self->plog( @_ , $name );
  7. };
  8. no strict qw( refs );
  9. *$name = $sub;
  10. }
  11. ...

or

  1. package Debug;
  2. sub callback { my $self = shift; $self->plog( @_, "callback" ) }
  3. sub log { my $self = shift; $self->plog( @_, "log" ) }
  4. sub printing { my $self = shift; $self->plog( @_, "printing" ) }
  5. ...

答案2

得分: 1

  1. $self->{ $file } = sub { log( @_, $file ) };

它被如下调用:

  1. $debug->{ callback }->( ... )
英文:

> put log function here?

You want to put a call to log there.

  1. $self->{ $file } = sub { log( @_, $file ) };

It's called as follows:

  1. $debug->{ callback }->( ... )

huangapple
  • 本文由 发表于 2023年2月27日 05:42:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定