On Aug 13, [EMAIL PROTECTED] said:

>Jeff 'Japhy' Pinyan wrote:
>> On Aug 13, [EMAIL PROTECTED] said:
>>
>>   my $data = shift;
>>   my $wrap_at = @_ ? shift : 75;
>>
>
>I like that.

The simpler-looking

  my $wrap_at = shift || 75;

has also been proposed.  The only reason I didn't use that is because, in
case you were ever in a situation where 0 was a valid value for your
variable, you wouldn't want to use that || operator.

>what is less awkward than [\s|\S] for 'match anything?'

Well, for certain values of "less awkward", /(?s:.)/, which is the .
metacharacter with the /s switch turned on -- that way it matches any
character including newlines.  I think someone suggested [.\n], but that
is not correct, because . just means "." inside a character class.
Another way to do it, if you are certain your input will have no
multi-byte characters, is to use \C, which matches a single byte.

>> EWW.  DON'T USE $`.  It's terrible.
>
>Okay, is that because it is slow and makes the rest of the regular
>expressions afterwards run slowly? (I saw something about that in the
>perlre document)

Yes.  When Perl compiles your program, it makes note of any use of $`, $&,
or $'.  If it sees you use it ONCE, ANYWHERE, it will make each regex
prepare their values for you.  Not cool.  What is cool is that, even
though $1, $2, etc. are the same way, they are provided only on a
per-regex basis.

>Is it as bad to use something like '(match anything)' before the main
>expression, and using $1 in place of $` when it's useful?

Well, if you're using a recent enough Perl (5.6+), you have access to the
@- and @+ arrays, which hold offsets related to your last successful
pattern match.  $-[0] holds the offset in your string where the match
started, so you could do

  my $pre = substr($str, 0, $-[0]);

to get the equivalent of $`.  See perlvar.

>I think I'll review all my material again on regexes.  Are there any
>good books you recommend on how to use and think in regexes?

"Mastering Regular Expressions", published by O'Reilly.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to