Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 2 Oct 2000 19:21:50 -0700
From:      Dirk Myers <dirkm@teleport.com>
To:        Christopher Rued <c.rued@xsb.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Perl question
Message-ID:  <20001002192150.C403@teleport.com>
In-Reply-To: <14808.63902.442934.667120@chris.xsb.com>; from c.rued@xsb.com on Mon, Oct 02, 2000 at 05:09:50PM -0400
References:  <14808.52583.347797.384055@chris.xsb.com> <20001002191537.G252@parish> <20001002192617.I252@parish> <39D8D5D9.67A3074B@mitre.org> <14808.63902.442934.667120@chris.xsb.com>

next in thread | previous in thread | raw e-mail | index | archive | help
"Christopher Rued" held forth that:

> When I run the following:
> 
>     #!/usr/bin/perl
>     $a = "xayxbyxcyxdy";
>     @s = $a =~ /x.y/;
>     print "\@s is @s\n";
> 
> I get:
>    
>     @s is 1
> 
> 
> 
> So, I seem to be getting the truth value rather than the first match
> in the string.

Actually, you get this list: (1).

Quoting perlop:

# If the /g option is not used, m// in a list context returns a list
# consisting of the subexpressions matched by the parentheses in the
# pattern, i.e., ($1, $2, $3...).

and:

# When there are no parentheses in the pattern, the return value is
# the list (1) for success.

> What confuses me is that if I specify the global option, I do not need
> to use a subexpression.  For example, if I run the following code:
> 
>     #!/usr/bin/perl
>     $a = "xayxbyxcyxdy";
>     @s = $a =~ /x.y/g;
>     print "\@s is @s\n";
> 
> I get:
> 
>     @s is xay xby xcy xdy

again, from perlop:

# The /g modifier specifies global pattern matching--that is,
# matching as many times as possible within the string.  How it
# behaves depends on the context.  In list context, it returns a list
# of all the substrings matched by all the parentheses in the regular
# expression.  If there are no parentheses, it returns a list of all
# the matched strings, as if there were parentheses around the whole
# pattern.


> So, this leaves me with a couple of questions, the main one being:
>   Why the different treatment for single matches and global
>   matches?

This one is easy:  because that's how they're defined to work. ;)

> 
> and a less important one:
>   Why is there no way to have the first match assigned to a scalar,
>   since we can be sure that there will be at most one match returned?

You're able to get it in other ways -- you can capture it in
parens, or you can use $& (and incur the associated performance
penalty).

If a match captured by default, though, there'd be no way to just
check whether a pattern matched without getting the full match 
returned  (which isn't a big deal for *this* pattern, but *might* be
a lot of stuff, which would be an unnecessary performance and memory
hit).   It's better to make someone actually ask for the string back
than to return it whether they want it or not.

Obligatory topic nag:  This really belongs on a perl list.

Obligatory RTFM nag:  This behavior is covered in the man pages, and
in the Camel.

Obligatory disclaimer:  I am not a perl guru.

Dirk	   dirkm@teleport.com
--  
There's a fine line between cleverness and idiocy.


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20001002192150.C403>