File: ArgNames.pm

package info (click to toggle)
libdevel-argnames-perl 0.03-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 68 kB
  • sloc: perl: 37; makefile: 2
file content (102 lines) | stat: -rw-r--r-- 1,976 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/perl

package Devel::ArgNames;

use strict;
use warnings;

our $VERSION = "0.03";

use base qw(Exporter);

our @EXPORT_OK = our @EXPORT = qw(arg_names);

use PadWalker qw(peek_my peek_our);

sub arg_names (;$) {
	my $level = shift || 2;
	{ package DB; () = caller($level - 1) }
	@{{ reverse %{peek_my($level)}, %{peek_our($level)} }}{\(@DB::args)};
}

__PACKAGE__

__END__

=pod

=head1 NAME

Devel::ArgNames - Figure out the names of variables passed into subroutines.

=head1 SYNOPSIS

	use Devel::ArgNames;

	sub foo {
		warn "foo() called with arguments: "
			. join(", ", map { defined() ? $_ : "<unknown>" } arg_names(@_) );
	}

	foo($bar, $gorch, $blah[4]);	

=head1 DESCRIPTION

When print-debugging code, you will often ind yourself going:

	print "\$foo is $foo, \$bar is $bar"

With this module, you can write a reusable subroutine easily:

	sub my_print_vars {
		my %vars;

		@vars{arg_names()} = @_;

		foreach my $var ( keys %vars ) {
			warn "$var is $vars{$var}\n";
		}
	}

	my_print_vars($foo, $bar);

This module doesn't provide dumping facilities because there are too many to
choose from. This is a DIY kit ;-)

=head1 EXPORTS

=over 4

=item arg_names [ $level ]

This function will return the names associated with the variables found on
C<@_>, at the level $level. If C<$level> is not provided C<arg_names>'s
caller's caller will be used (C<$level == 2> in that case).

=back

=head1 VERSION CONTROL

This module is maintained using Darcs. You can get the latest version from
L<http://nothingmuch.woobling.org/Devel-ArgNames/>, and use C<darcs send> to
commit changes.

=head1 SEE ALSO

L<Data::Dumper::Simple>, L<DB>, L<Devel::Caller>, L<Devel::Caller::Perl>,
L<PadWalker>.

=head1 AUTHORS

Ran Eilam

Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>

=head1 COPYRIGHT

	Copyright (c) 2007 Yuval Kogman. All rights reserved
	This program is free software; you can redistribute
	it and/or modify it under the same terms as Perl itself.

=cut