Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Deferring variables -- Getopt::Long

by Discipulus (Abbot)
on Jun 25, 2022 at 16:41 UTC ( #11145039=note: print w/replies, xml ) Need Help??

in reply to Deferring variables

Hello Chuma,

> number of parameters that can be set by the user

maybe I'm confused by your post but, if I understand it correctly, you are handling user provided params, if so I'd go totally other way: set defaults, overwrite them with user provided values, validate evrything with your own logic.

Then in the rest of your program you can safely use just one variable with the right name because it was a default or user provided or set by you following your logic

use 5.0010; use strict; use warnings; use Data::Dumper; use Getopt::Long; # provide sane defaults my %defaults = ( 'margin-left' => undef, 'margin-horizontal' => undef, 'margin' => 5, ); # use then user provided parameters unless ( GetOptions ( 'margin-left=i' => \$defaults{'margin +-left'}, 'margin-horizontal=i' => \$defaults{'margin +-horizontal'}, 'margin=i' => \$defaults{'margin +'}, )) {die "$0 usage..."} # validate them once for all validate_parameters(); print Dumper \%defaults; sub validate_parameters{ # here the logic of precedence for all paramenters $defaults{'margin-left'} //= $defaults{'margin-horizontal'} // $de +faults{'margin'}; } __END__ io@COMP:C>perl $VAR1 = { 'margin-horizontal' => undef, 'margin' => 5, 'margin-left' => 5 }; io@COMP:C>perl --margin-left 33 $VAR1 = { 'margin-horizontal' => undef, 'margin-left' => 33, 'margin' => 5 }; io@COMP:C>perl --margin-horizontal 4242 $VAR1 = { 'margin-horizontal' => 4242, 'margin-left' => 4242, 'margin' => 5 };


There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Deferring variables -- Getopt::Long
by LanX (Sage) on Jun 25, 2022 at 16:49 UTC
    I think the OP was talking about a CGI or similar web code.

    Though that can also be called from CLI ...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2022-12-02 09:38 GMT
Find Nodes?
    Voting Booth?