git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
	Thomas Rast <trast@student.ethz.ch>,
	Ryan Anderson <rda@google.com>,
	Jay Soffian <jaysoffian@gmail.com>
Subject: Re: [PATCH 13/16] send-email: extract_valid_address use qr// regexes and /o
Date: Thu, 30 Sep 2010 12:19:12 -0400	[thread overview]
Message-ID: <20100930161912.GA8707@sigill.intra.peff.net> (raw)
In-Reply-To: <1285854189-10240-14-git-send-email-avarab@gmail.com>

On Thu, Sep 30, 2010 at 01:43:06PM +0000, Ævar Arnfjörð Bjarmason wrote:

> Change the regex fragment in extract_valid_address to use the qr//
> syntax for compiled regexes, and when they're used add a /o flag so
> they're only compiled once for the lifetime of the program.
> [...]
>  sub extract_valid_address {
>  	my $address = shift;
> -	my $local_part_regexp = '[^<>"\s@]+';
> -	my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
> +	my $local_part_regexp = qr/[^<>"\s@]+/;
> +	my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
>  

Hmm. But these are lexical variables, so won't we recompile them each
time we enter the subroutine? I don't think it affects correctness, as
this "/o":

> +	return $address if ($address =~ /^($local_part_regexp)$/o);

means that we will compile and use the value from the first time we run
the function.

But we are unnecessarily compiling the sub-regexes each time. Not that
this is probably a performance critical piece of code, but your "/o" is
doing very little, and this is exactly the sort perl wankery that I find
interesting.

Sadly, there is no real perl equivalent of C static local variables,
which is what you really want.  Usually I would do:

  {
    my $local_part_regexp = qr/.../;
    sub extract_valid_address {
      ...
    }
  }

but beware of the execution order. That works well in a module, where
the module code is executed before anybody calls the function. But it
breaks in something like this:

  foo();
  {
    my $foo_static_local = 5;
    sub foo {
      print "$foo_static_local\n";
    }
  }

I think you could get by with:

  {
    my $local_part_regexp;
    sub extract_valid_address {
      $local_part_regexp ||= qr/.../;
      ...
    }
  }

-Peff

  reply	other threads:[~2010-09-30 16:19 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-30 13:42 [PATCH 00/16] git-send-email cleanups Ævar Arnfjörð Bjarmason
2010-09-30 13:42 ` [PATCH 01/16] send-email: use lexical filehandle for opendir Ævar Arnfjörð Bjarmason
2010-09-30 13:42 ` [PATCH 02/16] send-email: use lexical filehandles for $compose Ævar Arnfjörð Bjarmason
2010-09-30 13:42 ` [PATCH 03/16] send-email: use lexical filehandles during sending Ævar Arnfjörð Bjarmason
2010-09-30 13:42 ` [PATCH 04/16] send-email: get_patch_subject doesn't need a prototype Ævar Arnfjörð Bjarmason
2010-09-30 13:42 ` [PATCH 05/16] send-email: file_declares_8bit_cte " Ævar Arnfjörð Bjarmason
2010-09-30 13:42 ` [PATCH 06/16] send-email: unique_email_list " Ævar Arnfjörð Bjarmason
2010-09-30 13:43 ` [PATCH 07/16] send-email: cleanup_compose_files " Ævar Arnfjörð Bjarmason
2010-09-30 13:43 ` [PATCH 08/16] send-email: use \E***\Q instead of \*\*\* Ævar Arnfjörð Bjarmason
2010-09-30 13:43 ` [PATCH 09/16] send-email: sanitize_address use $foo, not "$foo" Ævar Arnfjörð Bjarmason
2010-09-30 13:43 ` [PATCH 10/16] send-email: sanitize_address use qq["foo"], not "\"foo\"" Ævar Arnfjörð Bjarmason
2010-09-30 13:43 ` [PATCH 11/16] send-email: use (?:) instead of () if no match variables are needed Ævar Arnfjörð Bjarmason
2010-09-30 13:43 ` [PATCH 12/16] send-email: is_rfc2047_quoted use qr// regexes Ævar Arnfjörð Bjarmason
2010-09-30 13:43 ` [PATCH 13/16] send-email: extract_valid_address use qr// regexes and /o Ævar Arnfjörð Bjarmason
2010-09-30 16:19   ` Jeff King [this message]
2010-09-30 16:33     ` Ævar Arnfjörð Bjarmason
2010-10-01  5:40       ` Jeff King
2010-09-30 17:25     ` Junio C Hamano
2010-09-30 18:56       ` Ævar Arnfjörð Bjarmason
2010-09-30 19:03   ` [PATCH v2 13/16] send-email: extract_valid_address use qr// regexes Ævar Arnfjörð Bjarmason
2010-09-30 13:43 ` [PATCH 14/16] send-email: send_message die on $!, not $? Ævar Arnfjörð Bjarmason
2010-09-30 13:43 ` [PATCH 15/16] send-email: make_message_id use "require" instead of "use" Ævar Arnfjörð Bjarmason
2010-09-30 13:43 ` [PATCH 16/16] send-email: use Perl idioms in while loop Ævar Arnfjörð Bjarmason
2010-09-30 14:30 ` [PATCH 00/16] git-send-email cleanups Brian Gernhardt
2010-09-30 14:52   ` Jeff King
2010-09-30 15:15     ` Brian Gernhardt
2010-09-30 15:11   ` Ævar Arnfjörð Bjarmason
2010-09-30 14:30 ` Jay Soffian
2010-09-30 16:21 ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100930161912.GA8707@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jaysoffian@gmail.com \
    --cc=rda@google.com \
    --cc=trast@student.ethz.ch \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).