behavior of m// operator

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

  1. Nathan

    Nathan Guest

    According to the documentation this program should print nothing since
    m// uses the last successful pattern match from m//, split, etc. However
    on my linux box it prints 1:

    #!/usr/bin/perl -w

    $a = "a";
    $b = "b";
    split /a/,$a;
    print $b =~ m//;

    Is the documentation wrong?

    -Nathan
    Nathan, Jul 30, 2008
    #1
    1. Advertising

  2. Nathan

    Zak B. Elep Guest

    Nathan <> writes:

    > According to the documentation this program should print nothing since
    > m// uses the last successful pattern match from m//, split, etc. However
    > on my linux box it prints 1:
    >
    > #!/usr/bin/perl -w
    >
    > $a = "a";
    > $b = "b";
    > split /a/,$a;
    > print $b =~ m//;
    >
    > Is the documentation wrong?


    No, it is correct. `print' emits 1 because the succeeding match
    operation ($b =~ m//) succeeded (m// essentially matches a `null'
    string, which $b has:

    ,----[ perl -de 0 ]
    | Loading DB routines from perl5db.pl version 1.28
    | Editor support available.
    |
    | Enter h or `h h' for help, or `man perldebug' for more help.
    |
    | main::(-e:1): 0
    | DB<1> ($a, $b) = ( qw(a b) )
    |
    | DB<2> x split /a/ => $a
    | empty array
    | DB<3> print $b =~ m//
    | 1
    | DB<4> x $b =~ m//
    | 0 1
    | DB<5>
    `----

    Because you used `=~', m// tries to match from $b, not from $a (really
    $_) as you would expect.

    --
    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 30, 2008
    #2
    1. Advertising

  3. Nathan

    Guest

    Nathan <> wrote:
    > According to the documentation this program should print nothing since
    > m// uses the last successful pattern match from m//, split, etc.


    My docs don't explicitly say whether the regex used in a split counts
    as previously "matched" or not in this context. Experimentally, it looks
    to me like it does not.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Jul 30, 2008
    #3
    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. Karlo Basic

    Unusual operator behavior

    Karlo Basic, Nov 10, 2003, in forum: C++
    Replies:
    11
    Views:
    565
    Karl Heinz Buchegger
    Nov 13, 2003
  2. Mantorok Redgormor
    Replies:
    70
    Views:
    1,734
    Dan Pop
    Feb 17, 2004
  3. The behavior of the operator %

    , Mar 18, 2005, in forum: C Programming
    Replies:
    8
    Views:
    279
    Richard Bos
    Mar 21, 2005
  4. noone
    Replies:
    5
    Views:
    304
    noone
    Jan 26, 2007
  5. Replies:
    4
    Views:
    527
Loading...

Share This Page