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

P

PerlFAQ Server

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top