我可以在glob运算符中使用Perl常量吗?

时间:2022-09-02 00:26:51

I'm parsing XML files with something like:

我正在解析XML文件,例如:

while (<files/*.xml>) { ... }

I want to use a constant to 'files', say

我想用常量来'文件'

use constant FILES_PATH => 'files';
while (<FILES_PATH/*.xml>) { ... }

I hope you understand the idea, and can help me :)..

我希望你理解这个想法,并可以帮助我:) ..

Thank you in advance.

先感谢您。

3 个解决方案

#1


Hmm, this is one reason to use Readonly instead of constant. You may be able to use & the start or () at the end of the constant to get Perl to realize it is subroutine. Let me check.

嗯,这是使用Readonly而不是常量的一个原因。您可以在常量的末尾使用&start或()来获取Perl以实现它是子例程。让我检查一下。

Nope, but you can use the classic trick of creating an arrayref to dereference:

不,但你可以使用创建arrayref的经典技巧来取消引用:

#!/usr/bin/perl

use strict;
use warnings;

use constant DIR => "/tmp";

print map { "$_\n" } <${[DIR]}[0]/*>;

But since glob "*" is the same as <*> you may prefer:

但由于glob“*”与<*>相同,您可能更喜欢:

#!/usr/bin/perl

use strict;
use warnings;

use constant DIR => "/tmp";

print map { "$_\n" } glob DIR . "/*";

I would probably say

我可能会说

#!/usr/bin/perl

use strict;
use warnings;

use Readonly;

Readonly my $DIR => "/tmp";

print map { "$_\n" } <$DIR/*>;

#2


As others have said, this is all about string interpolation.

正如其他人所说,这完全是关于字符串插值的。

The constant pragma fails in interpolative contexts, so you have to use something that will work.

常量编译指示在插值上下文中失败,因此您必须使用可行的内容。

As already mentioned, Readonly is one option.

如前所述,Readonly是一种选择。

You can also use the old "assign a read-only value to a typeglob trick".

您还可以使用旧的“将只读值分配给typeglob技巧”。

our $FILES_PATH; # declare FILES_PATH scalar to keep strict happy.
*FILES_PATH = \'files';  # Assign to typeglob so $FILES_PATH points to a string literal  

while( <$FILES_PATH/*.xml> ) {
    # read the files
}

#3


Surely "/" in this context would be treated as division and that is not likely to go anywhere. I think you might need to step back a bit and look at what other options you have. I think you are trying to do "file globbing" and I seem to recall support in perl for that though I do not recall the details. There is a "Glob" module in CPAN which you may want to look at. Personally I would be a lot more pedestrian and just use DirHandle and filter out non-xml files with an "next ... unless ..." line.

当然,在这种情况下,“/”将被视为分裂,而且不太可能在任何地方。我想你可能需要退后一步,看看你有什么其他选择。我认为你正试图做“文件通配”,我似乎回想起perl的支持,尽管我不记得细节了。 CPAN中有一个“Glob”模块,您可能需要查看它。就个人而言,我会更加行人,只需使用DirHandle并使用“next ... unless ...”行过滤掉非xml文件。

#1


Hmm, this is one reason to use Readonly instead of constant. You may be able to use & the start or () at the end of the constant to get Perl to realize it is subroutine. Let me check.

嗯,这是使用Readonly而不是常量的一个原因。您可以在常量的末尾使用&start或()来获取Perl以实现它是子例程。让我检查一下。

Nope, but you can use the classic trick of creating an arrayref to dereference:

不,但你可以使用创建arrayref的经典技巧来取消引用:

#!/usr/bin/perl

use strict;
use warnings;

use constant DIR => "/tmp";

print map { "$_\n" } <${[DIR]}[0]/*>;

But since glob "*" is the same as <*> you may prefer:

但由于glob“*”与<*>相同,您可能更喜欢:

#!/usr/bin/perl

use strict;
use warnings;

use constant DIR => "/tmp";

print map { "$_\n" } glob DIR . "/*";

I would probably say

我可能会说

#!/usr/bin/perl

use strict;
use warnings;

use Readonly;

Readonly my $DIR => "/tmp";

print map { "$_\n" } <$DIR/*>;

#2


As others have said, this is all about string interpolation.

正如其他人所说,这完全是关于字符串插值的。

The constant pragma fails in interpolative contexts, so you have to use something that will work.

常量编译指示在插值上下文中失败,因此您必须使用可行的内容。

As already mentioned, Readonly is one option.

如前所述,Readonly是一种选择。

You can also use the old "assign a read-only value to a typeglob trick".

您还可以使用旧的“将只读值分配给typeglob技巧”。

our $FILES_PATH; # declare FILES_PATH scalar to keep strict happy.
*FILES_PATH = \'files';  # Assign to typeglob so $FILES_PATH points to a string literal  

while( <$FILES_PATH/*.xml> ) {
    # read the files
}

#3


Surely "/" in this context would be treated as division and that is not likely to go anywhere. I think you might need to step back a bit and look at what other options you have. I think you are trying to do "file globbing" and I seem to recall support in perl for that though I do not recall the details. There is a "Glob" module in CPAN which you may want to look at. Personally I would be a lot more pedestrian and just use DirHandle and filter out non-xml files with an "next ... unless ..." line.

当然,在这种情况下,“/”将被视为分裂,而且不太可能在任何地方。我想你可能需要退后一步,看看你有什么其他选择。我认为你正试图做“文件通配”,我似乎回想起perl的支持,尽管我不记得细节了。 CPAN中有一个“Glob”模块,您可能需要查看它。就个人而言,我会更加行人,只需使用DirHandle并使用“next ... unless ...”行过滤掉非xml文件。