Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Passing complex html-tag input over command line to HTML TreeBuilder method look_down() properly

by bliako (Monsignor)
on Apr 18, 2019 at 09:55 UTC ( [id://1232750]=note: print w/replies, xml ) Need Help??


in reply to Passing complex html-tag input over command line to HTML TreeBuilder method look_down() properly

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.

  • Comment on Re: Passing complex html-tag input over command line to HTML TreeBuilder method look_down() properly
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1232750]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-18 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found