CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
'constant' in perl

 



DivyaG
Novice

Nov 9, 2009, 10:03 PM

Post #1 of 7 (4493 views)
'constant' in perl Can't Post

Can we define any variable as constant in perl (just like const in C and Java), so that its value cannot be modified throughout the program?


FishMonger
Veteran / Moderator

Nov 10, 2009, 5:23 AM

Post #2 of 7 (4475 views)
Re: [DivyaG] 'constant' in perl [In reply to] Can't Post

http://search.cpan.org/search?query=readonly+constant&mode=all


shawnhcorey
Enthusiast


Nov 10, 2009, 6:09 AM

Post #3 of 7 (4473 views)
Re: [DivyaG] 'constant' in perl [In reply to] Can't Post


In Reply To
Can we define any variable as constant in perl (just like const in C and Java), so that its value cannot be modified throughout the program?


No.

There are two techniques to get unmodifiable values in Perl: `use constant;` and `use Readonly;`

The pragmatic constant is part of the standard Perl installation and is available to any script. It creates a sub that returns the value. It can be used anywhere a sub can.

Code
perl -e 'use constant PI=>atan2(0,-1); print PI, "\n"'


Running the deparser on it, you can see the sub. You can also see that the compile phase replaces the sub with the value for faster execution.

Code
perl -MO=Deparse -e 'use constant PI=>atan2(0,-1); print PI, "\n"'


Readonly is a module available from CPAN: http://search.cpan.org/~roode/Readonly-1.03/Readonly.pm

What is does is tie into the variable and disables its ability to change. This adds some execution overhead every time the variable is used.

It biggest drawback is that Readonly must be installed on every system where the script runs.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


FishMonger
Veteran / Moderator

Nov 10, 2009, 7:11 AM

Post #4 of 7 (4472 views)
Re: [shawnhcorey] 'constant' in perl [In reply to] Can't Post


In Reply To
<snip>

Readonly is a module available from CPAN: http://search.cpan.org/~roode/Readonly-1.03/Readonly.pm

<snip>
It biggest drawback is that Readonly must be installed on every system where the script runs.


I'm not sure I'd put that in the "drawback" category. If we do, then we'd also have to put into that category all of the other very commonly used non-core modules, such as DBI or LWP:: or HTML::

A drawback of the constant pragma is that the constant can't be interpolated inside of a string like the Readonly var can.


shawnhcorey
Enthusiast


Nov 10, 2009, 7:26 AM

Post #5 of 7 (4470 views)
Re: [FishMonger] 'constant' in perl [In reply to] Can't Post


In Reply To
I'm not sure I'd put that in the "drawback" category. If we do, then we'd also have to put into that category all of the other very commonly used non-core modules, such as DBI or LWP:: or HTML::


Obviously, you never had to deal with a sysadmin that refuses to put any unapproved software on their machine because it might contain malware. And getting the software approved takes over a year. Sad but true.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


FishMonger
Veteran / Moderator

Nov 10, 2009, 7:38 AM

Post #6 of 7 (4468 views)
Re: [shawnhcorey] 'constant' in perl [In reply to] Can't Post


In Reply To
Obviously, you never had to deal with a sysadmin that refuses to put any unapproved software on their machine because it might contain malware. And getting the software approved takes over a year. Sad but true.


I guess I've been lucky since I am the sysadmin of a large portion of our systems, but I get your point.

Some time back a had to submit a request for some velcro and tie wraps to clean up some of the cabling in the NOC. Six months later is was rejected because I used the wrong form, even though it was the form that I was told to use. Go figure.


DivyaG
Novice

Nov 10, 2009, 8:15 PM

Post #7 of 7 (4448 views)
Re: [shawnhcorey] 'constant' in perl [In reply to] Can't Post

Thank you very much.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives