f



strange behavior of ??

Hi,

The definition of the m?? operator says that it only matches once 
between calls to reset. When I ran the following program I confirmed 
this behavior:

#!/usr/bin/perl
$_ = "Bilbo Baggins";
for($i=0;$i<5;$i++) {
     $a = ?Bilbo?;
}

That is, $a was set to 1 initially and then '' on every pass through the 
loop afterward. However, $a does not seem to be set to 1 at all in this 
program:

#!/usr/bin/perl
$_ = "Bilbo Baggins";
for (0..4) {
     $a = ?Bilbo?;
}

Initially $a is not defined and then it stays '' on each loop pass. I am 
running perl v5.8.8 on linux. My understanding is these two programs are 
supposed to be equivalent, so why am I observing different behavior with 
the debugger in the second one?

-Nathan
0
Nathan
7/28/2008 3:06:49 AM
comp.lang.perl.misc 33233 articles. 1 followers. brian (1246) is leader. Post Follow

5 Replies
483 Views

Similar Articles

[PageSpeed] 33

Nathan <user@serverrb.net> writes:

> #!/usr/bin/perl
> $_ = "Bilbo Baggins";
> for (0..4) {
>     $a = ?Bilbo?;
> }
>
> Initially $a is not defined and then it stays '' on each loop pass. I am
> running perl v5.8.8 on linux. My understanding is these two programs are
> supposed to be equivalent, so why am I observing different behavior with
> the debugger in the second one?

It is because you're aliasing the $_ to the elements of 0..4 in the
for() loop, which not what you expect when you assigned a string to it.
You should then use another scalar in the for() to alias the elements
to, for instance:

,----
| $_ = "Bilbo Baggins";
| for my $count ( 0 .. 4 ) {
|     $a = ?Bilbo?;
|     print "At $count, \$a is $a\n";
| }
`----

-- 
  I like the idea of 256 bits, though: 32 for the (Unicode) character leaves
  room for 224 Bucky bits, which ought to be enough for anyone.
				-- Roland Hutchinson, in alt.folklore.computers
0
zakame
7/28/2008 3:21:39 AM
Zak B. Elep wrote:
> Nathan <user@serverrb.net> writes:
> 
>> #!/usr/bin/perl
>> $_ = "Bilbo Baggins";
>> for (0..4) {
>>     $a = ?Bilbo?;
>> }
>>
>> Initially $a is not defined and then it stays '' on each loop pass. I am
>> running perl v5.8.8 on linux. My understanding is these two programs are
>> supposed to be equivalent, so why am I observing different behavior with
>> the debugger in the second one?
> 
> It is because you're aliasing the $_ to the elements of 0..4 in the
> for() loop, which not what you expect when you assigned a string to it.
> You should then use another scalar in the for() to alias the elements
> to, for instance:
> 
> ,----
> | $_ = "Bilbo Baggins";
> | for my $count ( 0 .. 4 ) {
> |     $a = ?Bilbo?;
> |     print "At $count, \$a is $a\n";
> | }
> `----
> 
  Thanks very much, that was very confusing to me. Perl does so many 
things automatically and behind the scenes I can't imagine coding in it 
*without* the debugger...

-Nathan
0
Nathan
7/28/2008 4:43:46 AM
Nathan <user@serverrb.net> writes:

>  Thanks very much, that was very confusing to me. Perl does so many
> things automatically and behind the scenes I can't imagine coding in it
> *without* the debugger...

`perl -de 0' provides a nice basic interactive perl.  Devel::REPL[1] is
also a fine alternative for quickly testing out/debugging code. :)


Footnotes: 
[1]  http://search.cpan.org/perldoc?Devel::REPL

-- 
  I like the idea of 256 bits, though: 32 for the (Unicode) character leaves
  room for 224 Bucky bits, which ought to be enough for anyone.
				-- Roland Hutchinson, in alt.folklore.computers
0
zakame
7/28/2008 5:08:37 AM
Nathan <user@serverrb.net> wrote:
*SKIP*
>  Thanks very much, that was very confusing to me. Perl does so many
>  things automatically and behind the scenes I can't imagine coding in
>  it *without* the debugger...

Add C<use strict> and C<use warnings>.  Or better B<carefully> read
documentation.

-- 
Torvalds' goal for Linux is very simple: World Domination
0
Eric
7/28/2008 7:20:07 PM
Nathan schreef:

> #!/usr/bin/perl
> $_ = "Bilbo Baggins";
> for (0..4) {
>      $a = ?Bilbo?;
> }

#!/usr/bin/perl -l
  use strict;
  use warnings;

  my $s = "Bilbo Baggins";

  for ( 0 .. 4 ) {
      print "$_ ",
            $s =~ m?Bilbo?
               ? "1"
               : "-",
      ;
  }
__END__

0 1
1 -
2 -
3 -
4 -


I (almost) never directly assign to $_ in my code, there is just no
need.

Always use both strict and warnings. Better not use $a or $b.

-- 
Affijn, Ruud

"Gewoon is een tijger."

0
Dr
8/1/2008 8:44:32 AM
Reply: