perl_constants

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



Fri Jul 21 19:09:20 2000

Some slashdot comments, on the subject of ways of doing 
constants in perl: 

http://slashdot.org/article.pl?sid=00/07/19/203221&mode=nested
Slashdot | Larry Wall Announces Perl 6

hy i love perl

   (Score:3, Insightful)
   by BlueLines on Wednesday July 19, @12:50PM PST
   (User #24753 Info) http://www.divisionbyzero.com

   First of all, here's a list of things i don't like (just so you know
   i'm being fair):
   1) The lack of constants: The only way to do constants is to create a
   subroutine that returns whatever value you want to be constant, ie:
   sub foo{ return "bar";}
   [ Reply to This | Parent | [Normal_____] ]

Re:why i love perl

   (Score:2)
   by Imperator (imperator@mytherDOESNOTLIKESPAM.com) on Wednesday July
   19, @01:23PM PST
   (User #17614 Info) http://myther.com/
   use constant foo => "bar\n";
   print foo; # or foo() under strict 'subs'
   [ Reply to This | Parent | [Normal_____] ]

 by Imperator (imperator@mytherDOESNOTLIKESPAM.com) on Wednesday July
   19, @01:23PM PST
   (User #17614 Info) http://myther.com/
   use constant foo => "bar\n";
   print foo; # or foo() under strict 'subs'
   [ Reply to This | Parent | [Normal_____] ]

Re:why i love perl

   (Score:3, Informative)
   by Iron_Slinger on Wednesday July 19, @01:24PM PST
   (User #126682 Info)

   You can also use the following.
   use constant FOO => 1;
   I believe the following also works. Check out "Advanced perl
   Programming" from the only Publisher out there for more info.
   *PI = \3.1415927;
   I've had probs with the last though.
   IS
   [ Reply to This | Parent | [Normal_____] ]
   
e:why i love perl

   (Score:1)
   by _marshall (marshall@culpepper.net) on Wednesday July 19, @03:51PM
   PST
   (User #71584 Info) http://ampd.virtualave.net
   "You can also use the following.
   use constant FOO => 1;" 
   This is fundamentally the same as an inline subroutine. Try this with
   your local perl interpreter:
   use constant FOO => ( "I", "like", "foo", "bar" );
   print FOO
   works like you would think. FOO is interpreted as a list and printed
   that way.
   But try this:
   print FOO[1]
   You get a "subscript on subroutine" error. The reason for this, is if
   you've ever taken a glance at the source code for constant.pm, your
   "constant" is loaded into your scripts namespace (more or less) as an
   inline subroutine. It's the exact same as saying:
   sub FOO{ return ( "I", "like", "foo", "bar" ) }
   So, In order to get the correct value you need to say:
   print ( (FOO)[1] ) 
   PS: sorry for the perl rant, but this needed some clarification :)
   [ Reply to This | Parent | [Normal_____] ]

Re:why i love perl


Re:why i love perl

   (Score:1)
   by elandal (Ismo.Peltonen@iki.fi) on Wednesday July 19, @09:50PM PST
   (User #9242 Info) http://www.iki.fi/~elandal
   > use constant FOO => 1;
   Is same as
   sub FOO { 1; }
   > *PI = \3.1415927;
   Assign reference to an anonymous variable into a typeglob. Not a
   constant either, and according to the article, typeglobs may be
   removed.
   Perl really doesn't have a way to declare a constant other than by
   creating a subroutine that consists of returning an anonymous variable
   - either with usual sub foo { bar; } or use constant foo => bar;
   I think that subroutine call that obviously (to the interpreter) does
   nothing but returns the same value every time it's called is optimized
   into an effective anonymous variable.
   But, as none of these really produces a constant - an immutable
   variable initialized in declaration - we can only hope it'll be added.
   [ Reply to This | Parent | [Normal_____] ]

Re:we:why i love perl

   (Score:2)
   by Abigail (abigail@delanet.com) on Wednesday July 19, @10:43PM PST
   (User #70184 Info) http://www.foad.org/%7Eabigail/
   Perl really doesn't have a way to declare a constant
   And neither does C for that matter, but you never hear someone
   complain about that. Pascal does have constants though.
   Of course, you might argue that C has

     # define FOO 1

   well, that isn't C, that's the C preproccessor. And you can use the
   same C preproccessor with Perl; just use the -P option.

   -- Abigail
   [ Reply to This | Parent | [Normal_____] ]

Re:why i love perl

   (Score:1)
   by Kickasso on Wednesday July 19, @11:49PM PST
   (User #210195 Info)

   const int foo = 10;
   int bar[foo];
   This is invalid C but valid C++. Which is, kinda, a form of complaint
   (why C doesn't have true constants? My language will!)
   [ Reply to This | Parent | [Normal_____] ]

Re:why i love perl

   (Score:1)
   by afc on Thursday July 20, @04:10AM PST
   (User #12569 Info) http://www.gnu.org
   Your Perl may be good Abigail, but your kung fu sucks, 'cause:
   const int foo = 0;
   Is valid ISO (ANSI, if you will) C, and we all know everybody uses
   ANSI C, right ;-)?
   [ Reply to This | Parent | [Normal_____] ]

Re:why i love perl

Re:why i love perl

   (Score:1)
   by jason_aw (jason_aw@bigfoot.com) on Wednesday July 19, @02:09PM PST
   (User #28317 Info)

   use constant FOO => 'bar';
   HTH, HAND.
   [ Reply to This | Parent | [Normal_____] ]

Re:why i love perl

   (Score:3, Informative)
   by Saucepan on Wednesday July 19, @02:10PM PST
   (User #12098 Info)

   You don't actually need an explicit return:
     sub foo { "bar" }
   This is actually how "use constant" works internally: it fakes up a
   subroutine that (implicitly) returns the requested value.
   Since the subroutine is defined at compile time, and the compiler sees
   that it returns a constant value, the constant value is inlined in
   place of the subroutine call.
   So, constants work and are exactly as fast as you'd expect, even
   though it may seem odd to people without backgrounds in functional
   languages that a constant is really a special case of a function, but
   with a built-in performance hack. :)
   [ Re:why i love perl

   (Score:1)
   by Nicolas MONNET (nico@nospam.monnet.to) on Wednesday July 19,
   @03:08PM PST
   (User #4727 Info) http://monnet.to

   sub foo { 'bar' }

   T's not that hard, isn't it? Think of it as "substite foo by bar". Now
   it makes sense. Slightly better:

   sub foo () { 'bar' }

   FWIW, YMMV, IANAL, FSCK.
   [ Reply to This | Parent | [Normal_____] ]

   (Score:0)
   by Anonymous Coward on Wednesday July 19, @10:00PM PST

   my $foo = \1.2345; # can't change this
   my $bar = \"Can't change this"; # ditto
   This is clearly said in eg. Perl Cookbook.
   [ Reply to This | Parent | [Normal_____] ]

===



the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu