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

Discussion in 'Perl Misc' started by PerlFAQ Server, Mar 10, 2011.

  1. 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}?

    You need to pass references to these objects. See "Pass by Reference" in
    perlsub for this particular question, and perlref for information on
    references.

    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
    Here's an example of how to pass in a string and a regular
    expression for it to match against. You construct the pattern with
    the "qr//" operator:

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

    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++) {
    $widget->$trick();
    }
    }

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

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

    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
    perlfaq.pod.
    PerlFAQ Server, Mar 10, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    493
    red floyd
    Nov 10, 2011
  2. Vsevolod Afanassiev

    How to put a filehandle into a hash array?

    Vsevolod Afanassiev, Jul 3, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    73
    Tad McClellan
    Jul 3, 2003
  3. PerlFAQ Server
    Replies:
    0
    Views:
    182
    PerlFAQ Server
    Jan 12, 2011
  4. PerlFAQ Server
    Replies:
    0
    Views:
    104
    PerlFAQ Server
    Jan 13, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    129
    PerlFAQ Server
    Jan 22, 2011
Loading...

Share This Page