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

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

This is an excerpt from the latest version perlfaq7.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .


7.15: 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", later in perlfaq7, for information on passing
    regular expressions.

    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 = <$passed_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).


The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in

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

Quoted text here. Click to load it

Regexen are passed as references too. qr//, like [], {} and sub {},
returns a ref to the compiled regex rather than the regex itself.

Strictly speaking it *is* possible to pass a regex directly, but the
only way to create one from Perl is to scalar-deref a qr//, and before
5.12 you have to take a ref again before you can do anything with it, so
it's not terribly useful.

Quoted text here. Click to load it

I think this bit could go. Anyone writing for 5004 knows they haven't
got qr//, and knows what they have to do instead.


Site Timeline