Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^4: Yet another config file editing programme : Tell me how to make it better !

by shmem (Chancellor)
on Sep 08, 2021 at 13:26 UTC ( [id://11136573]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Yet another config file editing programme : Tell me how to make it better !
in thread Yet another config file editing programme : Tell me how to make it better !

It seems that brevity and obfuscation in perl code are inseparable.

Not at all. Obfuscation and abbreviation are different concepts.

There is code in here and out there which is highly obfuscated but quite long, perhaps the most famous example being camel code. OTOH, there is perfectly readable code of unsurpassed succinctness and brevity, on CPAN as well as lurking here in the dungeons.

Problem is, brevity encapsulates concepts, quite like in mathematics. The concepts behind a Ricci tensor or a Christoffel symbol easily fill books. But these weren't invented for obfuscation, but abbreviation. You need to know the symbols, their meanings and the operations they allow in order to calculate with these high order concepts.

One perl builtin construct is a fine example - the diamond operator. Consider (taken from perlop):

while (<>) { # do something with $_ here }

This is equivalent to

unshift(@ARGV, '-') unless @ARGV; while ($ARGV = shift) { open(ARGV, $ARGV); while ($_ = readline(<ARGV>)) { # do something with $_ } }

So, the bare while(<>) { ... } is not an obfuscation, but an abbreviation for all the operations of its verbose version.

Or consider the following pieces of code, which do exactly the same thing:

%hash = (); { my @keys = qw ( a, b, c); my @values = 1..3; for ( my $c = 0; $c <= $#keys; $c++ ) { $hash { $keys [$c] } = $values [ $c ]; } }
@hash { qw (a, b, c) } = 1..3;

For me, readability and brevity would be discovering a module Unix::ConfigFile::DHCPCD, that included a method "UpdateInterfaceIP". I think that a valid metric for a modern language is the code I don't have to write.

Your expectation does not address readability and brevity, but language adoption and laziness. Perl does a very good job in doing as much for you as possible, as does CPAN, but it cannot by itself unite all different flavors of e.g. dhcp client configuration into a standard module. The maintainer of such a module would inherit all the technological debt of dhcp implementors. There are folks who try to do that, see https://www.webmin.com/. Download the https://download.webmin.com/download/modules/dhcpd.wbm.gz (which is a tar.gz) file, unpack it, and see what is needed for just the ISC DHCPD on various platforms. AFAIK they don't have support for DHCPCD client config, but feel free to submit a module.

It is the other way round: if all OS/platform vendors and DHCP implementors were to agree on perl as a standard configuration language, they would ship a Unix::ConfigFile::DHCPCD package which implements the method UpdateInterfaceIP whose body you would never look at, and wouldn't care about readability and brevity of its implementation, at all. But this ship has sailed far away and long ago.

Last point -

To an experienced perl programmer with deep knowledge of the language, this might look readable but for an occasional unprofessional perl user like myself, it would probably take at least half an hour to figure out what it is doing.
If I came back to this sort of code in a couple of years to alter/reuse it, I'd be back to square one.

Only use constructs you will never forget, and try to never forget what constructs you use.

Perl helps you with this, e.g. with the /x modifier for regular expressions. You could rewrite the tybalt89 code as

{ # block for local local $/ = ''; # paragraph mode path( $dhcpcdfile )->edit_lines( sub { if( m/ # match this: ^\s* # zero or more whitespace cha +rs at line begin profile # followed by the word "profi +le" \s+ # one or more whitespace static_ # then "static_" $ip_params{interface} # the value for key "interfac +e" in %ip_params \b # a word boundary .* # zero or more following char +s \n # and a newline /mx # in a multiline block ) { # alter this section s/ # match and substitute ^\s* # zero or more whitespace cha +rs at line begin static # word "static" \s+ # one or more whitespace char +s (\w+) # a word (see perlre) capture +d in $1 = # equal sign \K # but keep what was matched s +o far .* # any following chars / # against $ip_params{$1} # the value of key $1 (see ab +ove) in hash %ip_params /gmx; # globally in a multiline blo +ck (for "x" see perlre) } # endif } # end of sub ); } # end of "local $/" block

to explain what is meant today to the dumb-ass you'll be tomorrow. Just comment anything you might have forgotten later.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

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

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

    No recent polls found