in reply to Re: Return value from a binding operator?
in thread Return value from a binding operator?

it returns those submatches in list context.

But note that in this particular case, the context of the return statement — void, scalar, or list — is determined dynamically by the subroutine’s caller. Then, within the conditional operator ?:, the context “propagates downward into the 2nd or 3rd argument, whichever is selected.” (Conditional Operator). So if the sub is called in scalar context, and the condition evaluates to true, the regex will return true or false (i.e. 1 or 0) depending on whether it matches. On the face of it, this seems like a very poor design. :-(

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^3: Return value from a binding operator?
by LanX (Saint) on Apr 12, 2013 at 16:57 UTC
    Thx for spotting the same problem! ;)

    Here another version¹ independent from callers context!

    DB<138> sub Basename { my ($file) = $0 =~ m#/?([^/]+)$#; return $file; } DB<139> $0="a/b" => "a/b" DB<140> $a=Basename() => "b" DB<141> $0="b" => "b" DB<142> $a=Basename() => "b"

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    ¹) Just for didactic reasons, using a well tested basename-sub from CPAN is almost always better, File::Basename is even core!

    DB<153> use File::Basename DB<159> basename("a") => "a" DB<160> basename("a/b") => "b" DB<161> basename("a/b/c") => "c" DB<162> basename("a/") => "a"