Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Constant string reference

by przemo (Scribe)
on Dec 09, 2004 at 12:43 UTC ( [id://413500]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

One way of making scalar read-only is to assign to glob the reference to constant scalar, e.g.

*PI = \3.14;

However, I have a problem with constructing read-only strings. If

*NAME = \"My name";

is marked read-only,

*NAME2 = \"My $name";


Bigger snippet:

$bar = 'bar'; # This prints 'bar' *foo = \"The $bar is here"; $foo = "bar"; print "$foo\n"; # This dies *foo = \"The bar is here"; $foo = "bar2"; # error here print "$foo\n";

My question is: how to make "non-constant" strings (e.g. variables) become constant ones? Neither concatenation with '.', nor variable embedding in string works for me.

Replies are listed 'Best First'.
Re: Constant string reference
by diotalevi (Canon) on Dec 09, 2004 at 12:58 UTC

    This function is undocumented but comes with perl as of a 5.8.something version.

    Internals::SvREADONLY( \ $variable, 1 )
      This function is undocumented but comes with perl as of a 5.8.something version.
      Internals:: functions are not supposed to be used by user-code outside the core; that's why they're undocumented.


        No, that's not right. Internals::functions may be used by user-code but only by code that allows that perl internals are allowed to change from version to version. The B:: modules exist so that perl isn't a black box and to allow people access to the guts if they really need to be there.
Re: Constant string reference
by reneeb (Chaplain) on Dec 09, 2004 at 12:48 UTC
    use constant:
    #! /usr/bin/perl use constant{PI => 3.14, STRING => 'Test'}; print PI,"\n",STRING,"\n";
      I know I can use 'constant' pragma. However, I wanted to use plain scalar for some reasons.
Re: Constant string reference
by gellyfish (Monsignor) on Dec 09, 2004 at 12:56 UTC

    There is a readonly subroutine in Scalar::Util that you can use to do this simply, or If you have Inline available you can do this yourself trivially:

    use Inline C; + my $FOO = "Bar"; + readonly($FOO); + $FOO = "GG"; __DATA__ __C__ + void readonly(SV *sv) { SvREADONLY_on(sv); }


      Erm, Scalar::Util::readonly reports but doesn't set the readonly flag:

      readonly SCALAR Returns true if SCALAR is readonly. sub foo { readonly($_[0]) } $readonly = foo($bar); # false $readonly = foo(0); # true
Re: Constant string reference
by przemo (Scribe) on Sep 18, 2009 at 10:07 UTC

    Just as chronicler's duty -- which I hope explains this self-response -- I'd add there are Readonly and Readonly::XS modules which would serve the OP's purpose.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://413500]
Approved by Corion
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-07-14 07:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.