BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

I have a vague recollection of the addition of scoped pragmas in 5.10, but can't locate the docs?

My specific interest is whether it is now possible to have scoped constants?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re: 5.10: Are scoped constants possible?
by brian_d_foy (Abbot) on Dec 27, 2007 at 23:27 UTC

    For constants, perhaps you want Readonly, which can create lexically-scoped constant variables. The constant module just creates subroutines.

    As for scoped pragmas, we've had those for awhile (e.g. strict and warnings). Are you looking for new scoped pragmas? feature is new. The old pragmas bigint, bignum, bigrat, less and sort are now lexically scoped. See perldelta for the list of new pragmas.

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review

      Indirectly you led me to this extract from Perl 5.10 delta

      $^H and %^H

      The implementation of the special variables $^H and %^H has changed, to allow implementing lexical pragmas in pure Perl.

      And that leads to perlpragma which should allow what I want. Thanks.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        If you get this to work with constants, please do post how. It's not really clear to me how I would have to tweak the myint sample given in perlpragma to provide lexically scoped constants.
Re: 5.10: Are scoped constants possible?
by shmem (Chancellor) on Dec 27, 2007 at 23:06 UTC
    Given the quality of the perl documentation, if such was introduced, I'd expect a note in perldelta or the constant.pm pod. Since there's nothing in there, I'd say, alas, No.

    No scoped constant pragma (yet).

    Since constants are implemented (still) as subroutines subject to inlining which occupy a CODE slot in the symbol table, there's no such thing until lexical globs are implemented (you wrote the primary patch for that).

    Just trying reveals:

    #!/usr/local/bin/perl5.10.0 use constant FOO => 'quux'; { use constant FOO => 'bar'; } my $foo = FOO; print $foo,"\n"; __END__ Constant subroutine main::FOO redefined at /usr/local/lib/perl5/5.10.0 +/constant.pm line 115. bar

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: 5.10: Are scoped constants possible?
by quester (Vicar) on Dec 27, 2007 at 22:32 UTC
    It doesn't look like it. Exegesis 4 does have a lexical constant declaration,
    my $n is constant = 11;
    but my copy of Perl 5.10 doesn't like it,
    Bareword found where operator expected at scoped_const.pl line 5, near + "$n is" (Missing operator before is?)
      It doesn't look like it. Exegesis 4 does have a lexical constant declaration,

      my $n is constant = 11;

      but my copy of Perl 5.10 doesn't like it...

      This should not be too terribly surprising, since the Exegesis is about Perl 6, not Perl 5. In any case, that is an ancient document, and these days that particular syntax is invalid even in Perl 6, because we've split the concept of constantness in two. As noted elsewhere in this thread, you can't do constant folding unless the compiler actually knows what the constant is going to be at compile time. So we distinguish such true constants from the "fake" constants that are known to stay constant only over a given dynamic scope. Syntactically they are also distinguished:
      constant $n = 11;
      vs
      my $n is readonly = recalculate()
      Note that function parameters in Perl 6 default to readonly, not constant, since the actual value can vary from call to call, and hence cannot be constant-folded.

      In any case, if you want to quote Perl 6 documents, it's more useful to track the Synopses rather than the Exegeses, and do so at a more up-to-date site. (And, in fact, if you'd gotten your copy of Exegesis 4 from there, it would have had various annotations explaining where the Exegesis was out-of-date.)

        Mea culpa. I guess I was focusing too much on 5.10 marking the word "is" as a syntax error.

        Thanks very much for the pointer to the Perl 6 documentation!

        ... by the way, does anyone know why Google hasn't been indexing http://www.perlcabal.org/? That's the pits!

Re: 5.10: Are scoped constants possible?
by TedYoung (Deacon) on Dec 28, 2007 at 05:53 UTC

    Without trying anything here is some psudocode as to how it might be pulled off:

    package lexconstants; sub import { shift; my %h = @_; my $caller = caller; for my $name (keys %h) { *{$caller . "::$name"} = sub () { lexconstants::value($name) } unless defined &{$caller . "::$name"}; } $^H{lexconstants} = \ %h; } sub unimport { $^H{lexconstants} = undef; } sub value { my ($name) = @_; (caller(0))[10]{$name} or warn "Undefined constant $name..." }

    Again this is just psudocode (thinking out loud). This also assumes I know how to use %^H properly. I know it doesn't handle everything that constant does and it could cause sub conflicts in regions where no constant should have been defined. But, if you are using lex constants whose names mask package subs, you are probably crazy anyway. And, of course, you don't get the benifits of constant folding.

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)