FAQ
I think this is a bug:

tessio@krauzer:~/Perl/Ex/ch15$ cat when

#!/usr/bin/perl -w

use 5.010;

use strict;



given($ARGV[0]) {

when(/fred/i) { say 'Name has fred in it'; continue }

when(/^Fred/) { say 'Name starts with Fred'; continue }

when('Fred') { say 'Name is Fred' }

default { say "I don't see a Fred" }

}

tessio@krauzer:~/Perl/Ex/ch15$ ./when Fred

Name has fred in it

Name starts with Fred

Name is Fred

tessio@krauzer:~/Perl/Ex/ch15$ ./when Frederick

Name has fred in it

Name starts with Fred

I don't see a Fred

tessio@krauzer:~/Perl/Ex/ch15$

As you can see, the 'default' was not supposed to say anything in the second time the program is run, with the argument "Frederick"..
The last 'when' is supposed to have a hidden "break", right? So what's wrong?

Search Discussions

  • Chap Harrison at Nov 26, 2010 at 8:29 pm

    On Nov 26, 2010, at 1:10 PM, Téssio Fechine wrote:


    given($ARGV[0]) {

    when(/fred/i) { say 'Name has fred in it'; continue }

    when(/^Fred/) { say 'Name starts with Fred'; continue }

    when('Fred') { say 'Name is Fred' }

    default { say "I don't see a Fred" }

    } [snip]
    tessio@krauzer:~/Perl/Ex/ch15$ ./when Frederick

    Name has fred in it

    Name starts with Fred

    I don't see a Fred

    tessio@krauzer:~/Perl/Ex/ch15$

    As you can see, the 'default' was not supposed to say anything in the second time the program is run, with the argument "Frederick"..
    The last 'when' is supposed to have a hidden "break", right? So what's wrong?
    The third "when" is causing a string comparison, not a regular expression. 'Frederick' is not stringwise-equal to 'Fred', so the test fails, and you proceed into the default.

    The hidden "break" (like the explicit "continue") is only executed if the enclosing when-clause matches. Otherwise, you just proceed to test the next case - or into the default, as the case may be.

    Regards,
    Chap
  • Rob Dixon at Nov 26, 2010 at 8:29 pm

    On 26/11/2010 19:10, Téssio Fechine wrote:
    I think this is a bug:

    tessio@krauzer:~/Perl/Ex/ch15$ cat when

    #!/usr/bin/perl -w

    use 5.010;

    use strict;



    given($ARGV[0]) {

    when(/fred/i) { say 'Name has fred in it'; continue }

    when(/^Fred/) { say 'Name starts with Fred'; continue }

    when('Fred') { say 'Name is Fred' }

    default { say "I don't see a Fred" }

    }

    tessio@krauzer:~/Perl/Ex/ch15$ ./when Fred

    Name has fred in it

    Name starts with Fred

    Name is Fred

    tessio@krauzer:~/Perl/Ex/ch15$ ./when Frederick

    Name has fred in it

    Name starts with Fred

    I don't see a Fred

    tessio@krauzer:~/Perl/Ex/ch15$

    As you can see, the 'default' was not supposed to say anything in the
    second time the program is run, with the argument "Frederick".. The
    last 'when' is supposed to have a hidden "break", right? So what's
    wrong?
    Yes, but the last 'when' isn't executed because 'Frederick' doesn't
    equal 'Fred'. Neither the say nor the implicit break are executed and
    execution falls through to the default condition.

    HTH,

    Rob
  • Téssio Fechine at Nov 27, 2010 at 1:52 pm
    Another day, another problem.. =/
    --
    tessio@krauzer:~/Perl/Ex/ch15$ cat smart

    use 5.010;

    use strict;

    use warnings;



    my @divisors = (1, 2);

    say "It's divisible by 2!" if @divisors ~~ 2;

    tessio@krauzer:~/Perl/Ex/ch15$ perl smart

    tessio@krauzer:~/Perl/Ex/ch15$

    --

    Why "@divisors ~~ 2" is not true?
  • Magnus at Nov 27, 2010 at 2:31 pm

    my @divisors = (1, 2);

    say "It's divisible by 2!" if @divisors ~~ 2;
    How do you think that would work?
    From perldoc perlop:
    Binary "~~" does a smart match between its arguments. Smart
    matching is described in "Smart matching in detail" in perlsyn.

    my @divisors = (1 .. 6);
    for my $i(@divisors) {
    if(i % 2 == 0) {
    print "$i is divisible by 2\n";
    }
    }

    if(3 ~~ @divisors) {
    print "value 3 exists in \@divisors\n";
    }
  • Chap Harrison at Nov 27, 2010 at 3:01 pm

    On Nov 27, 2010, at 7:45 AM, Téssio Fechine wrote:

    --
    tessio@krauzer:~/Perl/Ex/ch15$ cat smart

    use 5.010;

    use strict;

    use warnings;



    my @divisors = (1, 2);

    say "It's divisible by 2!" if @divisors ~~ 2;

    tessio@krauzer:~/Perl/Ex/ch15$ perl smart

    tessio@krauzer:~/Perl/Ex/ch15$

    --

    Why "@divisors ~~ 2" is not true?
    No idea - it seems to work fine for me:
    - - -
    /Users/chap/private/perl$ cat smart
    use 5.010;
    use strict;
    use warnings;

    my @divisors = (1, 2);

    say "It's divisible by 2!" if @divisors ~~ 2;
    - - -
    /Users/chap/private/perl$ perl smart
    It's divisible by 2!

    /Users/chap/private/perl$ perl -v

    This is perl, v5.10.0 built for darwin-thread-multi-2level
    (with 2 registered patches, see perl -V for more detail)
  • Chap Harrison at Nov 27, 2010 at 3:49 pm

    On Nov 27, 2010, at 9:01 AM, Chap Harrison wrote:
    On Nov 27, 2010, at 7:45 AM, Téssio Fechine wrote:

    Why "@divisors ~~ 2" is not true?
    No idea - it seems to work fine for me:
    - - -
    /Users/chap/private/perl$ cat smart
    use 5.010;
    use strict;
    use warnings;

    my @divisors = (1, 2);

    say "It's divisible by 2!" if @divisors ~~ 2;
    - - -
    /Users/chap/private/perl$ perl smart
    It's divisible by 2!

    /Users/chap/private/perl$ perl -v

    This is perl, v5.10.0 built for darwin-thread-multi-2level
    Ah - are you running 5.12? I tried running the above with Perl 5.12, and replicated your behavior. Smart matching changed with 5.12:

    chap-VirtualBox:/home/chap/private/perl$ cat smart
    - - -
    use 5.010;
    use strict;
    use warnings;

    my @divisors = (1, 2);
    say "Array~~Scalar: It's divisible by 2!" if @divisors ~~ 2;
    say "Scalar~~Array: It's divisible by 2!" if 2 ~~ @divisors;
    - - -
    chap-VirtualBox:/home/chap/private/perl$ perl smart # running Perl 5.12.2
    Scalar~~Array: It's divisible by 2!

    Offhand I don't see exactly what accounts for the new behavior, apart from the non-commutativity that was introduced. It is probably covered here, but I'm not seeing it.

    http://search.cpan.org/~jesse/perl-5.12.2/pod/perl5120delta.pod#Smart_match_changes

    Regards, Chap
  • Téssio Fechine at Nov 27, 2010 at 6:16 pm
    Smart match operator was commutative the first time it was introduced.
    So "@divisors ~~ 2" would search for 2 in the list of divisors..
    For any reason smart match is not commutative anymore, so the right syntax now is "2 ~~ @divisors"..

    The problem is that the syntax has changed.

    De: magnus@trapd00r.se <magnus@trapd00r.se>
    Assunto: Re: smart match question
    Para: beginners@perl.org
    Data: Sábado, 27 de Novembro de 2010, 12:31

    How do you think that would work?
    From perldoc perlop:
    Binary "~~" does a
    smart match between its arguments. Smart
    matching is described
    in "Smart matching in detail" in perlsyn.


    my @divisors = (1 .. 6);
    for my $i(@divisors) {
    if(i % 2 == 0) {
    print "$i is divisible by 2\n";
    }
    }

    if(3 ~~ @divisors) {
    print "value 3 exists in \@divisors\n";
    }
  • Rob Dixon at Nov 27, 2010 at 7:58 pm

    On 27/11/2010 18:16, Téssio Fechine wrote:
    Smart match operator was commutative the first time it was introduced.
    So "@divisors ~~ 2" would search for 2 in the list of divisors..
    For any reason smart match is not commutative anymore, so the right syntax now is "2 ~~ @divisors"..

    The problem is that the syntax has changed.
    Yes. The smart match operator was commutative only in version 5.10 of
    perl. The change was made in the very next release - version 5.10.1.

    - Rob
  • Chap Harrison at Nov 27, 2010 at 11:55 pm
    Yes. The smart match operator was commutative only in version 5.10 of perl. The change was made in the very next release - version 5.10.1.
    Just wondering now -- does ...

    @divisors ~~ 2

    ... have any meaning now?

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedNov 26, '10 at 7:10p
activeNov 27, '10 at 11:55p
posts10
users4
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase