Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > strange behavior of ??

Reply
Thread Tools

strange behavior of ??

 
 
Nathan
Guest
Posts: n/a
 
      07-28-2008
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
 
Reply With Quote
 
 
 
 
Zak B. Elep
Guest
Posts: n/a
 
      07-28-2008
Nathan <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Zak B. Elep
Guest
Posts: n/a
 
      07-28-2008
Nathan <(E-Mail Removed)> 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
 
Reply With Quote
 
Eric Pozharski
Guest
Posts: n/a
 
      07-28-2008
Nathan <(E-Mail Removed)> 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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      08-01-2008
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."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thunderbird strange behavior... Jim Firefox 5 11-17-2005 03:09 PM
Firefox 1.04 and Strange Find Behavior Thomas Firefox 5 06-28-2005 08:40 PM
utf8 pragma - strange behavior ryang Perl 1 04-11-2005 05:38 AM
strange behavior when using 'read' sstark Perl 0 03-06-2005 02:27 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments