Hi together,

I stumbled across this problem today, while looking at some code using Test::Simple. The following test always passes, even if the string doesn't match:

ok(sub{$str =~ /abc/}, "string matches");

Now I can easily rewrite this test to:

ok($str =~ /abc/, "string matches");
Now it will only pass in case the string really matches. Problem solved.

But the question I'm really asking myself here is why did it pass in the first time? I wrote this simple example program:

#!/usr/bin/perl use strict; use warnings; (sub {my $str = "aaa"; $str =~ m/abc/}) ? print "sub ok\n" : print "su +b fail\n"; get_return() ? print "return ok\n" : print "return fail\n"; sub get_return { my $str = "aaa"; $str =~ m/abc/; }
The output is:
sub ok return fail
Can someone help me understand why the result of the anonymous (?) sub is true and the result of the regular sub is false?

In reply to Return value of a sub by elTriberium

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.