如何在哈希中重用子函数

时间:2017-09-20 11:36:45

标签: perl

use strict;
use warnings;

#My sample bbl content

my $sample = '
reflistStart
\bibtype{Article}%
\bibinfo{title}{Sample Title}
reflistEnd
reflistStart
\bibtype{Book}%
\bibinfo{title}{Sample Title}
reflistEnd
reflistStart
\bibtype{Proceedings}%
\bibinfo{title}{Sample Title}
reflistEnd
';

$sample=~s#reflistStart((?:(?!reflist(?:Start|End)).)*)reflistEnd#my $fulcnt=$&;
if($fulcnt=~m/\\bibtype\{article\}/i)
{
    $fulcnt = ArticleReplacement($fulcnt);
}
elsif($fulcnt=~m/\\bibtype\{book\}/i)
{
    $fulcnt = BookReplacement($fulcnt);
}
elsif($fulcnt=~m/\\bibtype\{proceedings\}/i)
{
    $fulcnt = ProceedingsReplacement($fulcnt);
}
($fulcnt);
#ges;

sub ArticleReplacement
{
    my $arttext = shift;
    $arttext=~s/\\bibinfo\{title\}/\\bibinfo\{articletitle\}/g;
    return $arttext;
}
sub BookReplacement
{
    my $arttext = shift;
    $arttext=~s/\\bibinfo\{title\}/\\bibinfo\{booktitle\}/g;
    return $arttext;
}
sub ProceedingsReplacement
{
    my $arttext = shift;
    $arttext=~s/\\bibinfo\{title\}/\\bibinfo\{proceedingstitle\}/g;
    return $arttext;
}

输出:

reflistStart
\bibtype{Article}%
\bibinfo{articletitle}{Sample Title} #title changed as articletitle
reflistEnd
reflistStart
\bibtype{Book}%
\bibinfo{Booktitle}{Sample Title}
reflistEnd
reflistStart
\bibtype{Proceedings}%
\bibinfo{Proceedingstitle}{Sample Title}
reflistEnd;

这里我正在为" IF"中的每种参考类型(文章,书籍,会议录)做条件。因此我的问题是,还有其他任何使用哈希的例子:

 my %Refstyles = ( 'article' => \&ArticleReplacement, 'book' => \&BookReplacement, ... );

1 个答案:

答案 0 :(得分:2)

确定。我想我明白。你的问题是这样的:

  

我有一些TeX文档,其中\bibtype{xxx}定义后跟一行\bibinfo{title}定义。

     

我需要使用title值更改bibinfo定义中的bibtype以确定替换文字。

     

我希望使用子例程引用的哈希来做到这一点,其中每个子例程都会更改一种bibtype

这是准确的吗?

如果是的话,我认为你过度复杂了。我认为你可以在没有子程序或引用或类似的东西的情况下做到这一点。我会这样写:

#!/usr/bin/perl

use strict;
use warnings;

# Hash that maps the bibtype to the title type
my %subs = (
  article     => 'articletitle',
  book        => 'booktitle',
  proceedings => 'proceedingstitle',
);

# Turn the keys of that hash into a regex
my $match = join('|', keys %subs);

my $sample = '
reflistStart
\bibtype{Article}%
\bibinfo{title}{Sample Title}
reflistEnd
reflistStart
\bibtype{Book}%
\bibinfo{title}{Sample Title}
reflistEnd
reflistStart
\bibtype{Proceedings}%
\bibinfo{title}{Sample Title}
reflistEnd
';

# A slightly complex substitution operator.
# We match all of the text that we're interested in (over two lines).
# We capture the bibtype and then replace the title with the new
# title string as looked up in our %subs hash.
# Another slight cleverness, is the use of \K and (?=...) to match
# bits of the string that we don't want to replace. See perldoc perlre
# for more details.
# Oh, and we use /g to change all of the titles in one go.
$sample =~ s/bibtype\{($match)\}%\n\\bibinfo\{\Ktitle(?=\})/$subs{lc $1}/eig;

print $sample;

运行此命令会提供以下输出:

reflistStart
\bibtype{Article}%
\bibinfo{articletitle}{Sample Title}
reflistEnd
reflistStart
\bibtype{Book}%
\bibinfo{booktitle}{Sample Title}
reflistEnd
reflistStart
\bibtype{Proceedings}%
\bibinfo{proceedingstitle}{Sample Title}
reflistEnd

哪个看起来对我来说。