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

Re: when to capitalize package variables

by haukex (Archbishop)
on Feb 06, 2021 at 08:48 UTC ( [id://11127969]=note: print w/replies, xml ) Need Help??


in reply to when to capitalize package variables

Just to throw another opinion into the mix: Sometimes one sees such variables strictly titlecased, since they're package variables but not constants, like for example the options in Data::Dumper (e.g. $Data::Dumper::Sparseseen or $Data::Dumper::Maxdepth). So for example $Data::GUID::Any::Uc may not seem to make as much sense as uc or UC, but it indicates it's a package variable since it's titlecased, but not a constant since it's not all uppercase. (I see kcott posted the same point as I was writing this)

The casing of variable names is just a convention and not a strict rule. Anyway, if you want my opinion on the following:

I need to provide a package variable in a module I'm working on to let the user turn on or off a feature. It's a simple non object oriented module. I've used Data::GUID::Any before and remembered there is a package variable called UC that is used exactly like what I want to do.

I would suggest not using a package variable at all. Despite the longer names, they're not really different from other globals like $/, and come with all the same potential issues: One has to remember to use local, and even then, dynamic scope might bite you lower down in the call stack. If you don't want an OO interface, then give your functional interface parameters to configure options.

use warnings; use strict; { package Foo; our $DEFAULT_BAR = 1; sub foo { my %opts = @_; $opts{bar} //= $DEFAULT_BAR; print "bar=$opts{bar}\n"; } } Foo::foo(); # prints "bar=1" Foo::foo(bar=>2); # prints "bar=2"

I usually set up package variables like $DEFAULT_BAR such that I can easily change the defaults, this can also be useful for testing, but I also usually leave them undocumented since I don't want to encourage the use of these globals. Also the above code could of course do some validation on foo's @_, but I omitted that for brevity.

Replies are listed 'Best First'.
Re^2: when to capitalize package variables
by eyepopslikeamosquito (Archbishop) on Feb 06, 2021 at 10:13 UTC

    I would suggest not using a package variable at all
    Agreed. I see in On Interfaces and APIs that Conway's SAT talks propose some sound general interface advice namely:
    Make common usage the default; allow uncommon usage via optional attributes
    ... with hashes being the natural way to implement that in Perl.

    In your Foo::foo(bar=>2) example, I note you used a hash rather than a hash ref. I've always used a hash ref, as recommended in PBP ... though, as indicated here, that was an error (compile-time vs run-time boo-boo) in the book from TheDamian - though he still stands by the advice.

    As a Perl beginner, I chose a simple non-OO interface in the Acme::EyeDrops sightly function way back in 2001, four years before PBP was published. I remember liking that interface at the time because it allowed me to add new attributes in the future without breaking old programs. Plus I've always hated global variables (in any language) and couldn't bring myself to create a public global variable in my first Perl module. :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found