From owner-freebsd-questions Mon Oct 2 19:21:43 2000 Delivered-To: freebsd-questions@freebsd.org Received: from smtp7.teleport.com (smtp7.teleport.com [192.108.254.51]) by hub.freebsd.org (Postfix) with SMTP id D9EB137B503 for ; Mon, 2 Oct 2000 19:21:39 -0700 (PDT) Received: (qmail 26310 invoked from network); 3 Oct 2000 02:21:32 -0000 Received: from pm3-01-04.cvo.du.teleport.com (HELO buster.dhis.org) (216.26.28.68) by smtp7.teleport.com with SMTP; 3 Oct 2000 02:21:32 -0000 Received: (from dirkm@localhost) by buster.dhis.org (8.9.3/8.9.2) id TAA05899; Mon, 2 Oct 2000 19:21:50 -0700 (PDT) (envelope-from dirkm) Date: Mon, 2 Oct 2000 19:21:50 -0700 From: Dirk Myers To: Christopher Rued Cc: freebsd-questions@freebsd.org Subject: Re: Perl question Message-ID: <20001002192150.C403@teleport.com> 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> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0.1i 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 X-System: FreeBSD 3.4-STABLE Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG "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