Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day Chuma,

I'd define your parameters and default values in a separate file. For this example, I've used JSON:

$ cat pm_11145036_parm_defaults.json { "margin-left" : [ null, "margin-horizontal" ], "margin-right" : [ null, "margin-horizontal" ], "margin-top" : [ null, "margin-vertical" ], "margin-bottom" : [ null, "margin-vertical" ], "margin-horizontal" : [ null, "margin" ], "margin-vertical" : [ null, "margin" ], "margin" : [ 10, null ] }

The following code is a rough example of how that might be used.

#!/usr/bin/env perl use strict; use warnings; use autodie; use constant { VALUE => 0, DEFER => 1, }; use JSON; my $json_file = 'pm_11145036_parm_defaults.json'; my $par = decode_json( do { local $/; open my $fh, '<', $json_file; <$fh>; } ); sub _get { my ($key) = @_; if (! defined $par->{$key}[VALUE]) { _get($par->{$key}[DEFER]) } else { return $par->{$key}[VALUE]; } } sub _set { my ($key, $value) = @_; if (! defined $par->{$key}[DEFER]) { warn "Can't change factory default: '$key'.\n"; } else { $par->{$key}[VALUE] = $value; } return; } # Some examples of usage: print 'margin-left = ', _get('margin-left'), "\n"; _set('margin-horizontal', 20); print 'margin-left = ', _get('margin-left'), "\n"; _set('margin-left', 30); print 'margin-left = ', _get('margin-left'), "\n"; print 'margin = ', _get('margin'), "\n"; _set('margin', 50); print 'margin = ', _get('margin'), "\n";

Output:

margin-left = 10 margin-left = 20 margin-left = 30 margin = 10 Can't change factory default: 'margin'. margin = 10

Notes:

  • Keeping your data separate means it can reused in all of your programs: no need to tweak %par in multiple scripts (potentially a lot of work; certainly error-prone).
  • When a user has set preferences, these can be stored in a separate file for reuse across all programs; e.g. parm_USERNAME.json.
  • For further reuse, put most of that code in a module; then each program just needs something like use Chuma::Layouts;.
  • I only used JSON as an example. It's not intended as a recommendation: pick whatever you want.

— Ken


In reply to Re: Deferring variables by kcott
in thread Deferring variables by Chuma

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-28 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found