develooper Front page | perl.perl5.porters | Postings from May 2000

Proposed pragma: readonly

From:
Mark Summerfield
Date:
May 20, 2000 06:37
Subject:
Proposed pragma: readonly
Message ID:
39269479.A29219C3@queenwood.screaming.net

"Andreas J. Koenig" wrote:
[snipped a lot of CPAN related stuff]
> I'd say for a pragma like readonly, you should seek to get approval
> from P5P. And I'd recommend scanning the archives because I recall,
> there were other proposals for how a readonly.pm should look like (Hi,
> Graham!). I believe, a readonly pragma is a good idea, and if the
> experts agree with your implementation, this would be a very good
> thing.
[snip]
(I didn't have any luck scanning the archives.)

I wanted to make I<scalars> readonly something that currently must be
done with this syntax:
	use vars '$BASE_PATH' ; 
	*BASE_PATH = \'/usr/local/lib/projectx' ;
(or via a tie which involves significant overhead).
Of course you can create subroutines via use constant that can be used
like constant scalars in many contexts, but they are I<not> constant
scalars.

Examples:
use readonly '$BASE_PATH' => '/usr/local/lib/projectx' ;
use constant   BASE_PATH  => '/usr/local/lib/projectx' ;

print "path: $BASE_PATH\n" ;     # Since its a scalar use as-is
print "path: @{[BASE_PATH]}\n" ; # For beginners?

$base = $Path{$BASE_PATH} ; # No prob.
$base = $Path{BASE_PATH} ;  # Oops, perl used literal 'BASE_PATH'
# Could use &BASE_PATH or BASE_PATH() instead.

$BASE_PATH = 4 ;
Fails with: Modification of a read-only value attempted

BASE_PATH  = 4 ; 
Fails with: Can't modify subroutine entry in scalar assignment

The readonly pragma as currently implemented will also take a list of
readonly's:

use readonly
	'$MAX_LINE_LEN'  => 80,
	'$MAX_LINES'     => 66,
	'$MAX_HEADLINES' =>  2,
	'$MAX_FOOTLINES' =>  3,
	; # dozens more etc.

The $ could be made optional but I included it 'cos I wanted usage to be
consistent throughout.

Of course what I'd really like is for readonlys to be part of the
language itself... but my C isn't up to it (& the more I use Perl the
more other languages seem like too much work before you even get to the
problem you want to solve...)

Since I've never proposed a pragma before I have no idea what happens? 

If readonly.pm is not approved I'm in a dilemma because on the one hand
it is a pragma & therefore should be all lowercase & on the other hand
all lowercase names are reserved the std distribution...

BTW the code is here:
http://www.cpan.org/modules/by-authors/id/S/SU/SUMMER/

_______________________________________________
Mark Summerfield       http://www.perlpress.com



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About