小编KJ7*_*LNW的帖子

我的所有 perl 模块 .pm 文件都需要 `$VERSION` 定义吗?

我刚刚向PDL::IO::Touchstone发行版添加了一个新文件,并注意到 CPAN 的索引器显示版本为 undef,因为$VERSION缺少:

     module : PDL::IO::MDIF
     version: undef
     in file: PDL-IO-Touchstone-1.009/lib/PDL/IO/MDIF.pm
     status : indexed
Run Code Online (Sandbox Code Playgroud)

所以 ::MDIF 没有$VERSION,但实际上它与 Makefile.PL 中注明的发行版相同:

     module : PDL::IO::MDIF
     version: undef
     in file: PDL-IO-Touchstone-1.009/lib/PDL/IO/MDIF.pm
     status : indexed
Run Code Online (Sandbox Code Playgroud)

问题:

  • 那么发行版中的这个模块需要一个版本吗?
  • 如果是这样,新模块是否应该与in提供的模块$VERSION分开维护? $VERSIONVERSION_FROMMakefile.PL
    • 我可以做$VERSION = $PDL::IO::Touchstone::VERSION,但不确定 CPAN 是否会解决这个问题。会吗?

我环顾四周,发现了很多关于版本控制实践的讨论,但没有任何关于同一 Perl 发行包中模块版本的讨论。请分享这里的最佳实践应该是什么,我是 Perl 模块的新手,这是我推出的第一个 2 文件发行版。

我确信在发布新的 dist 时我会更新主文件,但不确定当 dist 中其他模块发生更改时我是否会记得更改它们的版本。如果这里有一个低维护选项,那就太好了。

更新

我尝试了下面一些答案中的建议。这些都不起作用:

  • $VERSION = do { use PDL::IO::Touchstone; $PDL::IO::Touchstone::VERSION };

  • use PDL::IO::Touchstone; our $VERSION = $PDL::IO::Touchstone::VERSION; …

perl cpan version

7
推荐指数
2
解决办法
255
查看次数

在 Perl POD 中,链接到内部函数的正确方法是什么?

perlpod文档说可以,L<link to something>但它没有指出引用 Perl 核心函数的正确方法(或者如果确实如此,对我来说并不明显)。

具体来说,我想链接到 所显示的内容perldoc -f wantarray。链接到它的正确方法是什么,以便当您单击 MetaCPAN 和其他遵循链接的 POD 查看器中的链接时,L<...>它会将您带到文档?wantarray

(请注意,这wantarray只是一个内置的 Perl 函数,例如printor open。)

documentation perl perl-pod

4
推荐指数
1
解决办法
152
查看次数

你能在没有裸字 glob 的情况下 tie() 一个文件句柄吗?

我正在尝试使用Device::SerialPort而不使用裸字 glob,请参阅底部的问题。

这是他们的例子:

$PortObj = tie (*FH, 'Device::SerialPort', $Configuration_File_Name)
print FH "text";
Run Code Online (Sandbox Code Playgroud)

...但是用 *FH 污染命名空间感觉很脏...

tie(my $fh, ...)像你一样尝试过open (my $fh, ...),但Device::SerialPort没有实现TIESCALAR,所以它给出了一个错误。

这是我的肮脏黑客,但它仍然使用裸字,我什至无法让它限制裸字 glob 的范围:


# This does _not_ scope *FH, but shouldn't it?
{
        $port = tie(*FH, 'Device::SerialPort', $ARGV[0]) or die "$ARGV[0]: $!";
        $fh = \*FH;                      # as a GLOB
        $fh = bless(\*FH, 'IO::Handle'); # or as an IO::Handle
}

print $fh "text"; # this works

print FH "text";  # …
Run Code Online (Sandbox Code Playgroud)

perl serial-port file-handling tie

3
推荐指数
1
解决办法
85
查看次数

是否有与引用返回函数等效的“返回 NULL”?

我正在使用返回引用的现有代码,如下所示:

int &func()
{
   static int i = 5;
   return i;
}
Run Code Online (Sandbox Code Playgroud)

我想将错误情况返回为“NULL”,但当然 NULL 在按引用返回函数中不起作用。如果这返回一个指针我可能会这样做:

int *func()
{
   static int i = 5;

   if (!good)
       return NULL;
   else
       return &i;
}
Run Code Online (Sandbox Code Playgroud)

除了转换为基于指针的返回值之外,是否有返回失败状态的最佳实践?

c++ reference return-value return-by-reference

3
推荐指数
1
解决办法
167
查看次数

有没有办法定义包含预处理器指令的预处理器宏?

我想指示编译器使用如下代码展开一些循环。太长了,我不想复制粘贴。

#define 语句可以定义预处理器宏吗?

我试过这个:

#define foo \
    #ifdef __GNUC__                                             \
        #if __GNUC__ >= 8                                       \
            #pragma GCC unroll 128                              \
            #pragma GCC ivdep                                   \
        #endif                                                  \
    #endif                                                      \
    #ifdef __clang__                                            \
        #pragma clang loop vectorize(enable) interleave(enable) \
    #endif     
Run Code Online (Sandbox Code Playgroud)

但是当我foo在代码中使用时cpp显示它无效扩展为:

 #ifdef 4 #if 4 >= 8 #pragma GCC unroll 128 #pragma GCC ivdep #endif #endif #ifdef __clang__ #pragma clang loop vectorize(enable) interleave(enable) #endif
 #ifdef 4 #if 4 >= 8 #pragma GCC unroll 128 #pragma GCC ivdep #endif …
Run Code Online (Sandbox Code Playgroud)

c macros pragma c-preprocessor preprocessor-directive

2
推荐指数
1
解决办法
385
查看次数

在 Perl 中,您可以在没有“AUTOLOAD”的情况下子类化并挂钩所有父类函数吗?

我正在编写一个封装父类的多个对象的子类,这样我就可以像向量一样调用函数,如下所示:

package OriginalClass;

sub new { return bless {bar => 123}, 'OriginalClass' }

sub foo { return shift->{bar}; }
1;


package NewClass;
use parent OriginalClass;

# Return a blessed arrayref of "OriginalClass" objects.
# new() would be called NewClass->new(OriginalClass->new(), ...)
sub new { 
  my $class = shift;

  return bless \@_, 'NewClass';
}

# Vectorized foo(), returns a list of SUPER::foo() results:
sub foo
{
  my $self = shift;
  my @ret;

  push @ret, $_->SUPER::foo() foreach @$self;

  return @ret;
}

1;
Run Code Online (Sandbox Code Playgroud)

我不想 …

perl module subclass vectorization

1
推荐指数
1
解决办法
108
查看次数

v5.10 中的 Perl $AUTOLOAD 评估显示“在操作员期望的位置找到了 Bareword”...但 v5.26 工作正常

在这里使用 @ikegami 帖子AUTOLOAD中的示例。我的RF::Component::Multi模块最近的CPAN 测试人员报告指出:

Bareword found where operator expected at .../RF/Component/Multi.pm line 102, near "s/^.*:://sr"
syntax error at .../RF/Component/Multi.pm line 102, near "s/^.*:://sr"
Run Code Online (Sandbox Code Playgroud)

代码如下,在 GitHub

  • Perl 5.10 不喜欢什么?
  • 是否有一个需要 >5.10 的 Perl 功能隐藏在这里,我错过了?(我的 Perl 5.26.3 正在工作)
    • 如果是的话,可以使其向后兼容吗?如何?
    • 如果没有,我在哪里可以找到该版本以便我可以做正确的事情use 5.xx
  • 我需要use vars '$AUTOLOAD'
Bareword found where operator expected at .../RF/Component/Multi.pm line 102, near "s/^.*:://sr"
syntax error at .../RF/Component/Multi.pm line 102, near "s/^.*:://sr"
Run Code Online (Sandbox Code Playgroud)

perl autoload

1
推荐指数
2
解决办法
74
查看次数

perl subs 什么时候将参数作为 `$_` 传递,什么时候是 `$_[0]` ?

我注意到Parallel::Loops模块用作$_其 subref 的参数。这个问题与Parallel::Loops本身无关,而是与 coderef 调用约定有关。

这是他们的示例,请注意$_传递给子:

$pl->foreach( \@parameters, sub {
    # This sub "magically" executed in parallel forked child
    # processes
 
    # Lets just create a simple example, but this could be a
    # massive calculation that will be parallelized, so that
    # $maxProcs different processes are calculating sqrt
    # simultaneously for different values of $_ on different CPUs
    # (Do see 'Performance' / 'Properties of the loop body' below)
 
    $returnValues{$_} = …
Run Code Online (Sandbox Code Playgroud)

perl coderef

1
推荐指数
1
解决办法
168
查看次数