FAQ 7.14 How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.


7.14: How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?

With the exception of regexes, you need to pass references to these
objects. See "Pass by Reference" in perlsub for this particular
question, and perlref for information on references.

See ``Passing Regexes'', below, for information on passing regular

Passing Variables and Functions
Regular variables and functions are quite easy to pass: just pass in
a reference to an existing or anonymous variable or function:

func( \$some_scalar );

func( \@some_array );
func( [ 1 .. 10 ] );

func( \%some_hash );
func( { this => 10, that => 20 } );

func( \&some_func );
func( sub { $_[0] ** $_[1] } );

Passing Filehandles
As of Perl 5.6, you can represent filehandles with scalar variables
which you treat as any other scalar.

open my $fh, $filename or die "Cannot open $filename! $!";
func( $fh );

sub func {
my $passed_fh = shift;

my $line = <$fh>;

Before Perl 5.6, you had to use the *FH or "\*FH" notations. These
are "typeglobs"--see "Typeglobs and Filehandles" in perldata and
especially "Pass by Reference" in perlsub for more information.

Passing Regexes
To pass regexes around, you'll need to be using a release of Perl
sufficiently recent as to support the "qr//" construct, pass around
strings and use an exception-trapping eval, or else be very, very

Here's an example of how to pass in a string to be regex compared
using "qr//":

sub compare($$) {
my ($val1, $regex) = @_;
my $retval = $val1 =~ /$regex/;
return $retval;
$match = compare("old McDonald", qr/d.*D/i);

Notice how "qr//" allows flags at the end. That pattern was compiled
at compile time, although it was executed later. The nifty "qr//"
notation wasn't introduced until the 5.005 release. Before that, you
had to approach this problem much less intuitively. For example,
here it is again if you don't have "qr//":

sub compare($$) {
my ($val1, $regex) = @_;
my $retval = eval { $val1 =~ /$regex/ };
die if $@;
return $retval;

$match = compare("old McDonald", q/($?i)d.*D/);

Make sure you never say something like this:

return eval "\$val =~ /$regex/"; # WRONG

or someone can sneak shell escapes into the regex due to the double
interpolation of the eval and the double-quoted string. For example:

$pattern_of_evil = 'danger ${ system("rm -rf * &") } danger';

eval "\$string =~ /$pattern_of_evil/";

Those preferring to be very, very clever might see the O'Reilly
book, *Mastering Regular Expressions*, by Jeffrey Friedl. Page 273's
Build_MatchMany_Function() is particularly interesting. A complete
citation of this book is given in perlfaq2.

Passing Methods
To pass an object method into a subroutine, you can do this:

call_a_lot(10, $some_obj, "methname")
sub call_a_lot {
my ($count, $widget, $trick) = @_;
for (my $i = 0; $i < $count; $i++) {

Or, you can use a closure to bundle up the object, its method call,
and arguments:

my $whatnot = sub { $some_obj->obfuscate(@args) };
sub func {
my $code = shift;

You could also investigate the can() method in the UNIVERSAL class
(part of the standard perl distribution).


Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short. They represent an important
part of the Usenet tradition. They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile. If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release. It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.


Copyright (c) 1997-2002 Tom Christiansen and Nathan
Torkington, and other contributors as noted. All rights

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

Relevant Pages