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: Beginner:
constant in perl (use constant)?

 



perlFun
Novice

Jun 2, 2013, 4:57 PM

Post #1 of 5 (563 views)
constant in perl (use constant)? Can't Post

I'm checking out this document

http://perldoc.perl.org/constant.html

I am having a hard time understanding something simple. for example, why doesn't PI need to be $PI? Is the point that "constants" are something distinct from scalars, arrays, or hashes? But why is it that they can act like them? (as we see later in that document, they are defining a list, which I'm assuming they are using the word list interchangeably with array?) I'm sorry I'm just terribly confused. Also, why can you use the => syntax for example in the


use constant PI => 4 * atan(1,1);

I thought that => was used like a comma. So is this equivalent to


use constant PI, 4*atan(1,1);


I am confused by such a syntax. What is the comma doing here?


I don't expect someone to answer such a basic question for me in depth, so if you have any good resource where I could read something that will clear this up, I would be equally as greateful!


(This post was edited by perlFun on Jun 2, 2013, 4:58 PM)


hwnd
User

Jun 2, 2013, 7:31 PM

Post #2 of 5 (555 views)
Re: [perlFun] constant in perl (use constant)? [In reply to] Can't Post

This may help you. http://earino.wordpress.com/2013/02/27/use-constant-will-surprise-you-with-its-evil/


perlFun
Novice

Jun 2, 2013, 7:44 PM

Post #3 of 5 (553 views)
Re: [hwnd] constant in perl (use constant)? [In reply to] Can't Post

thanks for the link! unfortunately this is code I have to understand for work, so it's not code I'm designing and they are using constants all over the place :/


hwnd
User

Jun 2, 2013, 8:46 PM

Post #4 of 5 (547 views)
Re: [perlFun] constant in perl (use constant)? [In reply to] Can't Post

The => operator in perl is basically the same as comma. It's defined as the FAT comma. Therefore the left operand will be defined as a string if it begins with a letter or underscore, and is only letters, digits and underscores. Otherwise, the => operator behaves as the comma operator or list argument separator, according to context. You can use in a hash or when you pass arguments to a sub routine, etc.


Code
use constant FOO => "bar"; 
use constant BAR, "foo"; # This won't work if you 'use strict'
use constant 'BAR', 'foo'; # This will work if you 'use strict'

print "value of 'foo' is ", FOO, "\n";
print "value of 'bar' is ", BAR, "\n";

__OUTPUT__
value of 'foo' is bar
value of 'bar' is foo



2teez
Novice

Jun 2, 2013, 10:44 PM

Post #5 of 5 (540 views)
Re: [hwnd] constant in perl (use constant)? [In reply to] Can't Post

If I may add a 2 'kobo' note on what [ hwnd ] posted.

Code
use constant BAR, "foo";     # This won't work if you 'use strict'


As well as this:

Code
 my %h = ( president , 'obama');

WHY? Because the words in hash before the comma is a BAREWORD, which is not allowed while "strict subs" in use. So also is the 'BAR' as shown above.
Using the 'FAT COMMA' resolves this for the reason previously given.
However, this would work:

Code
use constant BAR(), "foo";     # This works

The writer of Perl Best Practice would argue in favour of [Readonly], others [Const::Fast] as a better alternative for using constant 'like' other programming language.


(This post was edited by 2teez on Jun 2, 2013, 10:57 PM)

 
 


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

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