DoubleBond has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'd like to seek the advice of the forum on a simple question

e.g.

my $parse = ' '; my $test_line = 'Happy Birthday'; $test_line =~/(Happy).*/; $parse = $1; print "parse = $parse\n";

Result would be:

parse = Happy
,but if I combine line 3 and 4 to:

$parse = $test_line =~/(Happy).*/;

The result would be:

parse = 1

I understand that '1' means 'true' in this case, but I don't understand why can't I get the text 'Happy'.

I see the following syntex all the time, but for some reasons it doesn't work for me.

$var = $line =~ /pattern/;

I tried looking for similar cases online, but couldn't find one.

Thank you so much,

DB

Replies are listed 'Best First'.
Re: Regex question
by Corion (Patriarch) on May 06, 2014 at 14:12 UTC

    It returns true or false because that's what =~ is documented to do.

    In list context, matching returns the list of captured elements. So you most likely see in your code:

    ($parse) = $test_line =~ /(Happy).*/;
Re: Regex question
by mr_mischief (Monsignor) on May 06, 2014 at 14:23 UTC

    You are being confused by list context vs. scalar context. Compare and contrast the version below.

    my $test_line = 'Happy Birthday'; my ( $parse1, $parse2 ) = $test_line =~ /(Happy)(.*)/; print "parse1 = $parse1\nparse2 = $parse2\n";

    The match in list context returns the matches. Excerpts from perlop:

    m/PATTERN/msixpodualgc /PATTERN/msixpodualgc Searches a string for a pattern match, and in scalar co +ntext returns true if it succeeds, false if it fails. If no +string is specified via the "=~" or "!~" operator, the $_ stri +ng is searched. (The string specified with "=~" need not be +an lvalue--it may be the result of an expression evaluatio +n, but remember the "=~" binds rather tightly.) See also perl +re.
    Matching in list context If the "/g" option is not used, "m//" in list context r +eturns a list consisting of the subexpressions matched by the parentheses in the pattern, that is, ($1, $2, $3...). +(Note that here $1 etc. are also set, and that this differs f +rom Perl 4's behavior.) When there are no parentheses in the pa +ttern, the return value is the list "(1)" for success. With o +r without parentheses, an empty list is returned upon fai +lure.

      Conversely, if the  /g modifier is used when matching in list context, regex behavior is changed yet again:

      c:\@Work\Perl>perl -wMstrict -le "my $line = 'Happy Birthday'; ;; my $p; ($p) = $line =~ /(Happy).*/; print qq{A: '$p'}; ;; ($p) = $line =~/Happy/g; print qq{B: '$p'}; " A: 'Happy' B: 'Happy'
Re: Regex question
by Laurent_R (Canon) on May 06, 2014 at 17:36 UTC
    I'll not repeat what has been said about this operator in list or scalar context, as this is clear enough IMHO, but if you are looking for brevity, you could combine lines 3, 4 and 5 of your first code snippet this way:
    print "parse = $1\n" if $test_line =~/(Happy).*/;
    And notice how the syntax here is explicitly using the Boolean value returned by the regex.
Re: Regex question
by DoubleBond (Initiate) on May 10, 2014 at 02:38 UTC
    Thank you all for the pointers! Problem solved :)