English 中文(简体)
• 如何在Perl?
原标题:How does =~ behave in Perl?
  • 时间:2012-04-09 11:15:29
  •  标签:
  • regex
  • perl

I have the following perl script:

$myVariable = "some value";
//... some code ...
$myVariable =~ s/+/ /g;
$myVariable =~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/seg;
//.. some more code ...

Reading from perl documentation, I think that =~ operator returns a boolean value, namely true or false.

我的问题是:如果涉及<代码>可兑换美元/代码>的上述业务,其价值是否受到影响。

页: 1 这是<条码>_ 易读物<>条码>的结果。 我认为这不应影响这一行动的行为。

P.S. If you need more code from my script just let me now.

最佳回答

。 如果你想要这一行动的丰硕成果,那么你需要分配。

$result = $myVariable =~ s/+/ /g;
问题回答

我们正在谈论这个问题。

leftvalue =~ rightvalue

权利价值必须是其中之一:

m/regexp/
s/regexp/replacement/
tr/regexp/translation/

留下的价值可以是左上的价值。

权宜之计<代码>left Value =~ rightwalue始终评价为保ool价值,但这一价值并没有被分配到留下价值! 这种 b的价值是表达本身的价值! 因此,如果出现这种情况,你可以使用非常罚款:

if (leftvalue =~ rightvalue) {
    // do something
}
  • m/regexp/ will never change anything. It just tests, if regexp matches on leftvalue.
  • s/regexp/replacement/ also tests if regexp matches on leftvalue, and if so, it replaces the matching part with replacement. If regexp did match, leftvalue =~ rightvalue is true, otherwise it is false.
  • tr/regexp/replacement/ analogously the same as s///, but with translation instead of replacement.

因此,这项工作将十分出色。

my @a=( acbc123 , aubu123 );
foreach (@a) {
    if ($_ =~ s/c(d)/x$1/g;) {
        $_ .=  MATCHED! ;
    }
}

结果是:

a[0] =  acbx123MATCHED! 

c 之后是一位数位数与正常表述相吻合。 So ist被X和该数字所取代。 既然如此,如果是这样的话,那是真实的,MATCHED! 页: 1

a[1] =  aubu123 

定期表达方式并不一致。 没有任何东西被取代,如果说错的话。

具有约束力的运营商只是“组合式”这一对一个运营商而言的目标变量。 它不影响价值。 替代操作者,s///,通常改变目标价值,并退还其替代物的数量。

 my $count = $target =~ s/.../.../;
 my $count = ( $target =~ s/.../.../ );  # same thing, clarified with ()

从Perl诉5.14起,替代操作者有/r的旗帜,只剩下目标值,而不是退回一个计数,将修改价值归为:

 my $modified = $target =~ s/.../.../r;

= ~本身指任何东西,它还需要对其左边变量做些什么。

如果看一个变量matches,那么你会使用m////m右侧,你或许会希望把这当作一种ool子,但你也可以从其他角度加以利用。 这不会改变oo:

$foo =~ m/pattern/

<>substitute 替代模式,您使用有关右侧的S/// ,这改变了 $:

$foo =~ s/pattern/replacement/;

为<>译注,在foo美元内,您使用权重///权重,这改变了 $:

$foo =~ tr/abc/def/;




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签