in reply to Return value from a binding operator?

> I can see what it's doing, but don't understand how the bind in $0 =~ m!/?([^/]+$)! strips the path off - or, in fact, returns anything.

In list context the match returns a list of those (grouped) submatches.

In this case everything "non /" [^/] between one or none slash /? and end of string $.

HTH =)

Cheers Rolf

( addicted to the Perl Programming Language)

EDIT: I'm not sure if it makes sense to put $ is within the group. Not my style...

UPDATE

CAREFUL!!!

This Basename() routine is dangerous cause it expects a scalar to be returned but only in list context!

DB<116> $0="a/b" => "a/b" DB<117> @a=Basename() => "b" # all grouped matches DB<118> $a=Basename() => 1 # true it matched

Plz be aware that the context is propageted to return.

UPDATE

thx Athanasius for spotting the same problem... race condition between my update and your post :)

Replies are listed 'Best First'.
Re^2: Return value from a binding operator?
by Athanasius (Archbishop) on Apr 12, 2013 at 16:35 UTC
    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,

      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"
Re: Return value from a binding operator?
by Spinone (Novice) on Apr 12, 2013 at 22:47 UTC

    Thanks.

    I was reading the second section as {match}{optional /}+{line beginning /} {not sure what $ meant}

    So that has helped some, but I still don't really get what that regex syntax does, particularly those square brackets [].

    I clearly have a lot to learn. Maybe there should be a "Absolute Perl Newbie, Don't be afraid to ask really dumb questions" section. :)

    My fault, not anyone elses. Thanks to everyone who replied.

    Chris

      > So that has helped some, but I still don't really get what that regex syntax does, particularly those square brackets [].

      it's a character class with just one element '/', which is negated b/c of leading '^'.

      That means: match any character except '/'.

      see perlretut Using-character-classes for details

      Cheers Rolf

      ( addicted to the Perl Programming Language)