use Getopt::Long; my %tags = (); my %cmd2tags = ( # list of cmd-line element types => correspondence to xpath (via look_down()) 'type' => '_tags', 'class' => 'class', 'id' => 'id', 'name' => 'name' ); GetOptions( "tags=s" => sub { # sub to be called whenever --tags XYZ is detected # it expects XYZ to be in the form "K=V" and K must # exist as a key in %cmd2tags my ($k,$v) = @_; if( $v =~ /^(.+?)=(.+?)$/ ){ my $t=$1; my $n=$2; my $c2t = $cmd2tags{$t}; die "unknown tag name '$t', I know only of ".join(",", keys %cmd2tags)."\n" unless defined $c2t; $tags{$c2t} = $n; } else { die "--tags V: V must be in the form 'key=value' where key can be 'class' or 'type', e.g. ..." } }, "url=s" => \$url, ) or die("Error in command line arguments. $!\n"); ... # now %tags contains tag-type-name=>value, e.g. '_tag' => 'div' etc. print "Tag received: '$_' => '".$tags{$_}."'\n" for(keys %tags); @results = $tree->look_down(%tags); ...