in reply to Passing Regex Pattern in Subroutine

I think you're looking for something more along these lines:
use warnings; use strict; my @ids = qw( john james ); my $permission = get_data( map { "^$_" } @ids ); print "$permission\n"; $permission = get_data( map { q{"\b} . $_ . q{\b"} } @ids ); print "$permission\n"; sub get_data { my (@patterns) = @_; my $permission; for (@patterns) { $permission .= qx( grep $_ /etc/passwd ); } return $permission; }

Replies are listed 'Best First'.
Re^2: Passing Regex Pattern in Subroutine
by Anonymous Monk on Aug 16, 2011 at 18:44 UTC
    Yes, this is what I am looking for. But I don't understand why you had to use 'map' to pass the regular expression.

    Is it because when you run get_data( map { "^$_" @ids } ), it is like doing get_data( "^john" ) and that is being passed to the subroutine?

      I don't understand why you had to use 'map' to pass the regular expression
      You could use map inside your sub, if you prefer.
      Is it because when you run get_data( map { "^$_" @ids } ), it is like doing get_data( "^john" ) and that is being passed to the subroutine?
      Not exactly. The map is like doing:
      get_data( '^john' , '^james' );