Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Quoting hash keys

by eyepopslikeamosquito (Archbishop)
on Nov 09, 2021 at 06:44 UTC ( [id://11138597]=note: print w/replies, xml ) Need Help??


in reply to Quoting hash keys

Bod, broader than just hashes, I encourage you to spend considerable time studying and thinking about cool ways to use Perl's many and varied ways of quoting! Time well spent. At least, this is one area where I've found Perl to be (easily) the most enjoyable language I've ever used. Perl's heredocs with variable interpolation, for example, I've found to be a joy compared to wrestling with Python's ugly triple-quoted multi-line strings.

Some examples from PBP Chapter 4 (Values and Expressions), will hopefully give you a feel for where I'm coming from.

While I tend to favour the q{form-of-quoting} by default, for example:

my $publisher = q{O'Reilly};

PBP gives an example showcasing how Perl allowing you to choose a different delimiter can clarify the code (by eliminating ugly escaping), making it easier to understand at a glance:

my $title = q[Perl Best Practices]; my $publisher = q[O'Reilly]; my $end_of_block = q[}]; my $closing_delim = q['}]; my $citation = qq[$title ($publisher)];

The fat-comma is also worthy of study. Conway, for example, finds this version more pleasing to the eye:

%default_service_record = ( name => '<unknown>', rank => 'Recruit', serial => undef, unit => ['Training platoon'], duty => ['Basic training'], );
than this one:
%default_service_record = ( 'name', '<unknown>', 'rank', 'Recruit', 'serial', undef, 'unit', ['Training platoon'], 'duty', ['Basic training'], );

References

Replies are listed 'Best First'.
Re^2: Quoting hash keys
by Bod (Parson) on Nov 10, 2021 at 01:19 UTC
    The fat-comma is also worthy of study.

    Yes, this is something I use a great deal.
    However, I pretty much always quote the LHS to aid clarity despite knowing that it is not necessary for alphanumeric strings.

      I pretty much always quote the LHS to aid clarity

      I pretty much always leave the LHS bare to aid clarity. If I see an alphanumeric string quoted before a fat comma it means I have to pause and wonder why the author did that just in case there is something that I might have missed on first glance. Avoiding the quoting effectively removes the question of why it is quoted.


      🦛

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-25 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found