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

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

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

问题

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

#!/usr/bin/env perl
#https://www.perl.com/article/closures-as-objects/
use warnings;
use strict;
use diagnostics;

package Debug;
use Data::Dumper;

sub new {
  my $self = {};
  # 如何将所有@files别名为子例程log?
  my @files = (qw(callback log printing));
  foreach my $file (@files) {
    # 使用ikegami提出的答案更新
    $self->{$file} = sub { plog( @_, $file ) };
  }
  print Dumper $self;
  return bless $self;
}

sub plog { # 提供Debug->log Debug->printing Debug->status等功能
    my ($self, $level, $message, $file) = @_;
    print "Level:'$level'\n";
    print "Message:'$message'\n";
    print "File:'$file'\n";
}

1;

###################################################

package main;

my $debug = Debug->new();

# 文件    等级,消息
$debug->plog(1, "Hello World", "在Debug中" );
$debug->callback(1, "Hello World" );
$debug->log(1, "Hello World" );
$debug->printing(1, "Hello World" );
#### 期望结果
# Level:1
# Message:Hello World
# 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!

#!/usr/bin/env perl                                                             
#https://www.perl.com/article/closures-as-objects/
use warnings;
use strict;
use diagnostics;

package Debug;
use Data::Dumper;
sub new {
  my $self = {};
  # How do I alias all @files to sub log ?
  my @files = (qw(callback log printing ));
  foreach my $file (@files){
    # updated with ikegami's proposed answer
    $self->{$file} = sub { plog( @_, $file ) }; 
  }
  print Dumper $self;
  return bless $self;
}

sub plog { # provides Debug->log Debug->printing Debug->status etc..
    my ($self, $level, $message, $file) = @_;
    print "Level:'$level'\n";
    print "Message:'$message'\n";
    print "File:'$file'\n";
    
}

1;

###################################################

package main;
  


my $debug = Debug->new();

#       file        level, message
$debug->plog(1,"Hello World","were in the Debug" );
$debug->callback(1,"Hello World" );
$debug->log(1,"Hello World" );
$debug->printing(1,"Hello World" );
#### Desired result
# Level:1
# Message:Hello World
#File:callback

答案1

得分: 2

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

package Debug;

use Sub::Name qw( subname );

for my $name (qw( callback log printing )) {
   my $sub = subname $name, sub {
      my $self = shift;
      return $self->plog( @_ , $name );
   };

   no strict qw( refs );
   *$name = $sub;
}

...

或者

package Debug;

sub callback { my $self = shift; $self->plog( @_, "callback" ) }
sub log      { my $self = shift; $self->plog( @_, "log"      ) }
sub printing { my $self = shift; $self->plog( @_, "printing" ) }

...
英文:

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

package Debug;

use Sub::Name qw( subname );

for my $name (qw( callback log printing )) {
   my $sub = subname $name, sub {
      my $self = shift;
      return $self->plog( @_ , $name );
   };

   no strict qw( refs );
   *$name = $sub;
}

...

or

package Debug;

sub callback { my $self = shift; $self->plog( @_, "callback" ) }
sub log      { my $self = shift; $self->plog( @_, "log"      ) }
sub printing { my $self = shift; $self->plog( @_, "printing" ) }

...

答案2

得分: 1

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

它被如下调用:

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

> put log function here?

You want to put a call to log there.

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

It's called as follows:

$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:

确定