Maybe your command-line logic can be changed so as to differentiate between element type, name, class and id? Right now you have adopted a convention which says : use the same parameter key (--tags) for type and class but whenever there is a class you need to throw in a class=... in order to differentiate between them.

How about --tags type=div --tags class='comic-' --tags id='123'. An alternative would be: --tags-type div --tags-class 'comic-' --tags-id '123'. Meaning you created 3 different command line options and would need to keep adding if you remember more tag things like --tags-name.

Once you have the command-line logic that suits you, your users AND ALSO (most importantly) allows for expanding your features in the future without changing the API too much, then perhaps you want to use Getopt::Long's GetOptions() feature of passing a sub to parse complext command-line values. So that you keep command line parsing inside GetOptions() and don't "pollute" the rest of your code with command-line checks and parsing. For example (assuming you picked the first "cmd-line logic" i proposed):

use Getopt::Long; my %tags = (); my %cmd2tags = ( # list of cmd-line element types => correspondence to xpath (via lo +ok_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); ...

Edit: IMO relying on the order of command line parameters is not good form. There are exceptions of course, but I always try to avoid it if I can. In your use-case it is not required. And the code I showed does not require it either.


In reply to Re: Passing complex html-tag input over command line to HTML TreeBuilder method look_down() properly by bliako
in thread Passing complex html-tag input over command line to HTML TreeBuilder method look_down() properly by sadarax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.