#!/usr/bin/perl

use 5.006;

use strict;
use warnings;

use Getopt::Long 2.33 qw{ :config auto_version };
use Pod::Usage;
use PPI::Document;
use PPIx::QuoteLike::Utils qw{ __variables };

our $VERSION = '0.024';

my %opt;

GetOptions( \%opt,
    help => sub { pod2usage( { -verbose => 2 } ) },
) and @ARGV == 1 or pod2usage( { -verbose => 0 } );

my $doc = PPI::Document->new( -e $ARGV[0] ? $ARGV[0] : \$ARGV[0] );
{
    local $\ = "\n";
    print for map { $_->[0] }
	sort { $a->[2] cmp $b->[2] || $a->[1] cmp $b->[1] }
	map { [ $_, substr( $_, 0, 1 ), substr( $_, 1 ) ] }
	__variables( $doc );
}

__END__

=head1 TITLE

variables - List the variables in a chunk of Perl

=head1 SYNOPSIS

 variables eg/variables
 variables 'qr/foo$bar/smx'
 variables -help
 variables -version

=head1 OPTIONS

=head2 -help

This option displays the documentation for this script. The script then
exits.

=head2 -version

This option displays the version of this script. The script then exits.

=head1 DETAILS

This Perl script lists the variables in the chunk of Perl specified as
the only command argument. This can be either the name of a file or
literal Perl code; the choice is a pragmatic one based on whether a file
by the given name actually exists.

The output is in alphabetical order by variable name, and within
variable name by sigil. In the case of subscripted variables, the sigil
displayed will be that of the underlying variable. That is, if
C<$foo[0]> appears, C<@foo> will be displayed.

This script will find variables appearing in the code, and variables
interpolated into strings. If L<PPIx::Regexp|PPIx::Regexp> can be
loaded, variables interpolated into regular expressions will also be
found.

The heavy lifting is done by
L<PPIx::QuoteLike::Utils|PPIx::QuoteLike::Utils> subroutine
L<__variables()|PPIx::QuoteLike::Utils/__variables>.

B<NOTE> that the C<__variables()> subroutine is discouraged, and may be
deprecated and removed.

=head1 AUTHOR

Thomas R. Wyant, III F<harryfmudd at comcast dot net>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2019-2022, 2026 by Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5.10.0. For more details, see the full text
of the licenses in the files F<LICENSE-Artistic> and F<LICENSE-GPL>.

This program is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.

=cut

# ex: set textwidth=72 :
