strange behavior of ??

Discussion in 'Perl Misc' started by Nathan, Jul 28, 2008.

  1. Nathan

    Nathan Guest

    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
    Nathan, Jul 28, 2008
    #1
    1. Advertising

  2. Nathan

    Zak B. Elep Guest

    Nathan <> 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
    Zak B. Elep, Jul 28, 2008
    #2
    1. Advertising

  3. Nathan

    Zak B. Elep Guest

    Nathan <> 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
    Zak B. Elep, Jul 28, 2008
    #3
  4. Nathan <> 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
    Eric Pozharski, Jul 28, 2008
    #4
  5. Nathan

    Dr.Ruud Guest

    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."
    Dr.Ruud, Aug 1, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sstark
    Replies:
    0
    Views:
    461
    sstark
    Mar 6, 2005
  2. ryang
    Replies:
    1
    Views:
    940
    Wes Groleau
    Apr 11, 2005
  3. Apogee

    Strange Behavior with ViewState

    Apogee, Jul 3, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    325
    Apogee
    Jul 3, 2003
  4. PJ

    DropDownList Strange Behavior

    PJ, Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    352
  5. Mantorok Redgormor
    Replies:
    70
    Views:
    1,751
    Dan Pop
    Feb 17, 2004
Loading...

Share This Page