Hi,

2007/1/22, Ovid <[EMAIL PROTECTED]>:

[snip]

  my $captured;
  if ( $some_var =~ /^(\w+)\s+/ ) {
      $captured = $1;
  }
  if ( $captured ) { ... }

> I also noticed that $capture here will always contain the first catched
> match ($1).

No, it doesn't in your example.  The only way to make that work would be to
use 'list context' and default to matching the '$_' variable:

  my ($capture) = /^(\w+)\s+/;

Note that because we're using parentheses on the left side, that forces list
content and because we're using the assignment operator, '=', instead of the
binding operator, '=~', the regular expression matches against '$_' instead
of the left hand side.  This is probably a bit more of an advanced usage,
though.  You might use it like this:

  while (<FH>) {
      next if /^#/;                  # skip comments
      my ($capture) = /^(\w+)\s+/;   # grab first 'word'
      ...
  }


I also like to point that you still can match $some_var like this:

my ($capture) = $some_var =~ m/^(\w+)\s+/;

Example:

while (my $some_var = <FH>) {
   next if $some_var =~ /^#/;
   my ($capture) = $some_var = m/^(\w+)\s+/;
   ...
}

[snip]

HTH!

--
Igor Sutton Lopes <[EMAIL PROTECTED]>

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to