找到最后一个“}”子程序的

发布于 2024-10-02 05:53:37 字数 374 浏览 8 评论 0原文

假设我有一个带有 Perl 代码的文件:有人知道是否有一个模块可以找到该文件中某个子例程的结束“}”。 例如:

#!/usr/bin/env perl
use warnings;
use 5.012;

routine_one( '{°^°}' );

routine_two();

sub routine_one {
    my $arg = shift;
    if ( $arg =~ /}\z/ ) {
    say "Hello my }";
    }
}

sub routine_two {
    say '...' for 0 .. 10
}

该模块应该能够删除整个routine_one,或者它应该可以告诉我该例程中结束“}”的行号。

Supposed I have a file with Perl-code: does somebody know, if there is a module which could find the closing "}" of a certain subroutine in that file.
For example:

#!/usr/bin/env perl
use warnings;
use 5.012;

routine_one( '{°^°}' );

routine_two();

sub routine_one {
    my $arg = shift;
    if ( $arg =~ /}\z/ ) {
    say "Hello my }";
    }
}

sub routine_two {
    say '...' for 0 .. 10
}

The module should be able to remove the whole routine_one or it should can tell me the line-number of the closing "}" from that routine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

豆芽 2024-10-09 05:53:37

如果您要解析 Perl 代码,则需要使用 PPI

You want to use PPI if you are going to be parsing Perl code.

飘逸的'云 2024-10-09 05:53:37
#!/usr/bin/env perl
use warnings;
use 5.012;
use PPI;

my $file = 'Example.pm';

my $doc = PPI::Document->new( $file );
$doc->prune( 'PPI::Token::Pod' );
$doc->prune( 'PPI::Token::Comment' );
my $subs = $doc->find( sub { $_[1]->isa('PPI::Statement::Sub') and $_[1]->name eq 'layout' } );
die if @$subs != 1;

my $new = PPI::Document->new( \qq(sub layout {\n    say "my new layout_code";\n}) );
my $subs_new = $new->find( sub { $_[1]->isa('PPI::Statement::Sub') and $_[1]->name eq 'layout' } );


$subs->[0]->block->insert_before( $subs_new->[0]->block ) or die $!;
$subs->[0]->block->remove or die $!;


# $subs->[0]->replace( $subs_new->[0] );
# The ->replace method has not yet been implemented at /usr/local/lib/perl5/site_perl/5.12.2/PPI/Element.pm line 743.

$doc->save( $file ) or die $!;
#!/usr/bin/env perl
use warnings;
use 5.012;
use PPI;

my $file = 'Example.pm';

my $doc = PPI::Document->new( $file );
$doc->prune( 'PPI::Token::Pod' );
$doc->prune( 'PPI::Token::Comment' );
my $subs = $doc->find( sub { $_[1]->isa('PPI::Statement::Sub') and $_[1]->name eq 'layout' } );
die if @$subs != 1;

my $new = PPI::Document->new( \qq(sub layout {\n    say "my new layout_code";\n}) );
my $subs_new = $new->find( sub { $_[1]->isa('PPI::Statement::Sub') and $_[1]->name eq 'layout' } );


$subs->[0]->block->insert_before( $subs_new->[0]->block ) or die $!;
$subs->[0]->block->remove or die $!;


# $subs->[0]->replace( $subs_new->[0] );
# The ->replace method has not yet been implemented at /usr/local/lib/perl5/site_perl/5.12.2/PPI/Element.pm line 743.

$doc->save( $file ) or die $!;
小伙你站住 2024-10-09 05:53:37

如果您的子例程不包含任何空行(如示例中的空行),则以下内容将起作用:

#!/usr/bin/perl -w

use strict;

$^I = ".bkp";  # to create a backup file

{
   local $/ = ""; # one paragraph constitutes one record
   while (<>) {
      unless (/^sub routine_one \{.+\}\s+$/s) {  # 's' => '.' will also match "\n"
         print;
      }
   }
}

The following will work in case your subroutines don't contain any blank lines, like the one in your example:

#!/usr/bin/perl -w

use strict;

$^I = ".bkp";  # to create a backup file

{
   local $/ = ""; # one paragraph constitutes one record
   while (<>) {
      unless (/^sub routine_one \{.+\}\s+$/s) {  # 's' => '.' will also match "\n"
         print;
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文