Re: hashes + use constant - weird behavior

2010-05-14 Thread Dr.Ruud

Stanisław T. Findeisen wrote:


use constant {
SOME_CONSTANT => 'some value'
};




$hash{SOME_CONSTANT} = 'value 1';


Not weird at all, just works as documented.



So change to either:

  $hash{ +SOME_CONSTANT } = 'value 1';

or:

  $hash{ SOME_CONSTANT() } = 'value 1';


--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: use constant

2009-03-16 Thread Chas. Owens
On Mon, Mar 16, 2009 at 11:16, Suzanne Aardema  wrote:
snip
> I do this regularly redefine constants in my programs. I'm not sure if it's 
> good practice but I do it.
>
> What I do is define a constant in most subroutines. The constant is called, 
> strange enough, PROC_NM. I felt that because this was being defined local to 
> each routine that I wasn't breaking all the rules I had been taught. I know I 
> get a warning message each time I redefine it but to me it is a local 
> constant not a variable. I don't define it in my helper routines so when they 
> printout an error message and reference PROC_NM I know where they are called 
> from.
>
> If anyone has a better idea I'd love to hear it.
snip

It sounds like you need Readonly::XS[1].  The constant pragma creates
constants by writing a subroutine like

sub PI() { 3.14 }

Subroutines are global in scope, so it doesn't matter that you are
using the constant only in one function.  Readonly::XS creates
constants by turning on a bit in the scalar that makes the variable
read only (hence its name).  Since this is a normal scalar, it can
have the scope you desire for it.  The Readonly[2] module also allows
you to create read only hashes and arrays (at the cost of some speed).

#!/usr/bin/perl

use strict;
use warnings;

use Readonly;

func1();
func2();

sub func1 {
Readonly my $PROC_NM => (caller 0)[3];
print "I am in $PROC_NM\n";
}

sub func2 {
Readonly my $PROC_NM => (caller 0)[3];
print "I am in $PROC_NM\n";
}

1. http://search.cpan.org/dist/Readonly-XS/XS.pm
2. http://search.cpan.org/dist/Readonly/Readonly.pm

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: use constant

2009-03-16 Thread Suzanne Aardema
- Original Message -
From: "Paul Johnson" 
To: "Stanisław T. Findeisen" 
Cc: beginners@perl.org
Sent: Thursday, 12 March, 2009 19:27:02 GMT -07:00 US/Canada Mountain
Subject: Re: use constant

On Thu, Mar 12, 2009 at 11:50:46PM +0100, "Stanisław T. Findeisen" wrote:

> Is there any way to change the values of [scalar/array] constants  
> defined via "use constant" pragma?

That seems a strange thing to want to do.  But useful for redefining pi, or
perhaps G, I suppose.

In general the answer is no.  But if you can live with a mandatory warning and
the knowledge that you have lied to perl (and you understand the effects that
brings, specifically with respect to constant propagation) then go ahead and
do it.

It's not a good idea, but it's not in perl's nature to pretend that she knows
better than you, even if she does.

If you want a constant that varies, can you not just use a variable?

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

--

I do this regularly redefine constants in my programs. I'm not sure if it's 
good practice but I do it.

What I do is define a constant in most subroutines. The constant is called, 
strange enough, PROC_NM. I felt that because this was being defined local to 
each routine that I wasn't breaking all the rules I had been taught. I know I 
get a warning message each time I redefine it but to me it is a local constant 
not a variable. I don't define it in my helper routines so when they printout 
an error message and reference PROC_NM I know where they are called from.

If anyone has a better idea I'd love to hear it.


Suzanne. 
- 
Suzanne Aardema 
Systems Analyst/Programmer 
Applications Development 
Computing Services 
Athabasca University 
1 University Drive 
Athabasca, AB T9S 3A3 

ph: 780-421-2527 
fax: 780-428-2464 
email: suzan...@athabascau.ca 

__ 
This communication is intended for the use of the recipient to whom it
is addressed, and may contain confidential, personal, and or privileged
information. Please contact us immediately if you are not the intended
recipient of this communication, and do not copy, distribute, or take
action relying on it. Any communications received in error, or
subsequent reply, should be deleted or destroyed.
---

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: use constant

2009-03-12 Thread Paul Johnson
On Thu, Mar 12, 2009 at 11:50:46PM +0100, "Stanisław T. Findeisen" wrote:

> Is there any way to change the values of [scalar/array] constants  
> defined via "use constant" pragma?

That seems a strange thing to want to do.  But useful for redefining pi, or
perhaps G, I suppose.

In general the answer is no.  But if you can live with a mandatory warning and
the knowledge that you have lied to perl (and you understand the effects that
brings, specifically with respect to constant propagation) then go ahead and
do it.

It's not a good idea, but it's not in perl's nature to pretend that she knows
better than you, even if she does.

If you want a constant that varies, can you not just use a variable?

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




use constant

2009-03-12 Thread Stanisław T. Findeisen
Is there any way to change the values of [scalar/array] constants 
defined via "use constant" pragma?


STF

===
http://eisenbits.homelinux.net/~stf/ . My PGP key fingerprint is:
9D25 3D89 75F1 DF1D F434  25D7 E87F A1B9 B80F 8062
===

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: hashes + use constant - weird behavior

2009-03-06 Thread Chas. Owens
On Fri, Mar 6, 2009 at 03:31, "Stanisław T. Findeisen"
 wrote:
> Chas. Owens wrote:
>>
>> SOME_CONSTANT is being interpreted as the string "SOME_CONSTANT".
>
> Why is it so? This is crazy.

Because it is nicer to say $hash{key} than $hash{"key"} and Perl is
optimized for the common case.  When you look at any language you find
design choices that are odd.  The constants manufactured by the
constant pragma are really 0-ary functions:

sub SOME_CONSTANT() { return "some value" }

>
>> This is one of the drawbacks to the constant pragma.  Change the code
>> hash keys to one of these and it will work the way you want it to:
>>
>> $hash{+SOME_CONSTANT} #unary plus
>
> What is this unary plus?
snip

Unary plus is the opposite of unary minus: -10, +10.  In Perl 5 unary
plus doesn't do anything to its argument regardless of what type its
arguments has (list, number, string, reference, etc.).  This makes it
useful as an aid to telling Perl's parser you don't mean what it would
normally assume.  For instance, examine this code:

print ("x" x 10), " foo ", ("x" x 10);

It is obvious to a human that we want to print all three items, but
Perl only prints the first.  This is because Perl sees the first set
of parentheses as part of the function call.  With the addition of
unary plus:

print +("x" x 10), " foo ", ("x" x 10);

Perl can see that the first parentheses are not part of the function
call because + is not allowed in the middle of a function call and
unary plus (by definition of the operator) has no affect on ("x" x
10), so the code executes the way want it to.  It is also one of the
characters that is not allowed in a "bareword".  Barewords do not need
to be quoted in hashes and before the fat comma (=>).  This is because
it looks nicer that way and most keys are barewords.


snip
> Thanks. Hmm, they say it is slow
> (http://search.cpan.org/dist/Readonly/Readonly.pm#CONS).
snip

No, they say constant hashes and arrays are slow, scalars are as fast
as normal scalars.  This is because scalars are implemented using
normal Perl scalars with a flag set that sets to raise an error if
anyone tries to modify it.  Hashes and arrays are implemented through
tie and therefore have an extra layer of indirection associated with
each access.

snip
> Why aren't there constants in the language itself?? This is crazy.
> Also: is that true that Perl's specification is its implementation by Larry
> Wall? This is even more crazy. :-/
> A language and its implementation are (should be) 2 different things!
snip

For the same reason the const keyword wasn't part of K&R C: nobody
thought about it at the time.  Later people realized they wanted
constants and created a facility to get them.  You seem to be laboring
under the impression that languages should be designed carefully for
years, then implemented.  Perl 1 was little more than a commandline
utility, people found it useful, but limited, so they started adding
features until we got here.  This means there are lots of blind alleys
in the design.  Constants can be considered one.  The way prototypes
work in Perl is another.  The benefit of this style of development is
that it encouraged experimentation with the language without leading
to multiple incompatible languages.  Find me a non-trivial Common Lisp
program that doesn't rely on one specific implementation of Common
Lisp.  How do I make an non-blocking read of one character in ANSI C?
Specifications tend to be incomplete and slow to evolve.  So
implementers start making custom changes.  And now you have multiple
languages that mostly look like each other but behave differently in
some cases.

Perl 6 is different.  As a community, we have stepped back and are
reexamining the language and attempting to make a clean specification
that retains the flavor of Perl 5 while correcting its defects.  There
are already competing implementations of the incomplete spec (Rakudo,
Pugs, etc.).  Time will tell if having multiple implementations turns
out to be a good or bad thing (my bet is that it will be a mixed bag
of bad and good).

snip
> I'd expect equal hash keys to map to equal values.
> I'm a conservatist. :-)
snip

Ah, but they do, you just didn't hand the hash equal values.


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: hashes + use constant - weird behavior

2009-03-06 Thread Jenda Krynicky
From:   "Stanisław T. Findeisen"

> Chas. Owens wrote:
> > SOME_CONSTANT is being interpreted as the string "SOME_CONSTANT".
>
> Why is it so? This is crazy.

Because most often you want it that way. Most often you do want the
$hash{BLAHBLAH} to mean $hash{'BLAHBLAH'} and not have to put quotes
around the hash keys. Same thing in

%hash = (
somekey => 87754,
otherkey => 71816,
);

> > This is one of the drawbacks to the constant pragma.  Change the code
> > hash keys to one of these and it will work the way you want it to:
> >
> > $hash{+SOME_CONSTANT} #unary plus
>
> What is this unary plus?

It's a "noop" = "no operation", that prevents the parser from using
the rule that says that if the contents of the {} look like word,
they should be treated as if the word was quoted.

> Why aren't there constants in the language itself?? This is crazy.

Define "in the language itself".

Constants in the form of inlined function were in the language for
ages, the "constant" pragma is a relatively new invention that does
little more than the original

sub SOME_CONSTANT () { 'value 1' }

such functions are inlined into the place they are called from during
the optimization phase of compilation. This means that the optimizer
may continue with the optimization, evaluate expressions

sub PI () {3.1415927}
$O = 2*PI*$r; ==>   $O = 6.2831854*$r;

remove unreachable code:

   use constant DEBUG => 0;
   print Dumper($dataStructure) if DEBUG; ==>  

etc.

> Also: is that true that Perl's specification is its implementation by
> Larry Wall? This is even more crazy. :-/
> A language and its implementation are (should be) 2 different things!

The implementation is open sourced and ported to prettymuch anywhere.
It had even been forked temporarily at times. And the specification
is not so much the implementation, but rather its tests. And there's
plenty of them. Isn't it great to have an executable specification?
One you can test easily? Instead of immersing yourself into a bunch
of papers and attempting to convince yourself that what you
implemented matches the specification, you run the test suite and
KNOW.

Besides ... the implementation was not written by Larry. Maybe back
then when it was Perl 1, but the current Perl is written and
maintained by a lot more people.

> Gunnar Hjalmarsson wrote:
> > So, what behavior exactly is it that you consider weird? What output had 
> > you expected?
>
> I'd expect equal hash keys to map to equal values.
> I'm a conservatist. :-)

Not really. You expected $hash{BLAH} and $hash{'BLAH'} to mean
different things. They don't. They are parsed exactly the same.

Jenda
= je...@krynicky.cz === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: hashes + use constant - weird behavior

2009-03-06 Thread Paul Johnson
On Fri, Mar 06, 2009 at 09:31:44AM +0100, "Stanisław T. Findeisen" wrote:

> Chas. Owens wrote:
>> SOME_CONSTANT is being interpreted as the string "SOME_CONSTANT".
>
> Why is it so? This is crazy.

Were it not so, every time you created a sub with the same name as one
of your hash keys you would find the result of your sub being used
instead of the hash key.  This is called action at a distance and is
generally thought to be a bad thing.

>> This is one of the drawbacks to the constant pragma.  Change the code
>> hash keys to one of these and it will work the way you want it to:
>>
>> $hash{+SOME_CONSTANT} #unary plus
>
> What is this unary plus?

The opposite of a unary minus.  In this case it is a trick to let the
parser know that SOME_CONSTANT is not a bareword.  It's not the solution
I would use.

>> $hash{&SOME_CONSTANT} #function call method 1
>> $hash{SOME_CONSTANT()} #function call method 2

This one is.

>> However, I would suggest using the ReadOnly* module for constants instead.
>>
>> * http://search.cpan.org/dist/Readonly/Readonly.pm
>
> Thanks. Hmm, they say it is slow  
> (http://search.cpan.org/dist/Readonly/Readonly.pm#CONS).

But is it too slow for you?  If all you wanted was speed of execution
you would have chosen another language, right?  And did you read the
second paragraph there?

> Why aren't there constants in the language itself?? This is crazy.

I suppose the answer to that depends on your definition of "constant",
"in" and "crazy".

> Also: is that true that Perl's specification is its implementation by  
> Larry Wall? This is even more crazy. :-/

You're really going to have to tell us what you mean by "crazy".  If you
just mean that you'd prefer a full specification and multiple
implementations then you might be pleased with the direction that Perl 6
is taking.  Otherwise, the current situation seems to have worked pretty
well for the last 21 years.

> A language and its implementation are (should be) 2 different things!

Or perhaps not?

> Gunnar Hjalmarsson wrote:
>> So, what behavior exactly is it that you consider weird? What output had you 
>> expected?
>
> I'd expect equal hash keys to map to equal values.
> I'm a conservatist. :-)

Then you'll be pleased to know that perl is not crazy in this respect,
at least.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: hashes + use constant - weird behavior

2009-03-06 Thread Stanisław T. Findeisen

Chas. Owens wrote:

SOME_CONSTANT is being interpreted as the string "SOME_CONSTANT".


Why is it so? This is crazy.


This is one of the drawbacks to the constant pragma.  Change the code
hash keys to one of these and it will work the way you want it to:

$hash{+SOME_CONSTANT} #unary plus


What is this unary plus?


$hash{&SOME_CONSTANT} #function call method 1
$hash{SOME_CONSTANT()} #function call method 2

However, I would suggest using the ReadOnly* module for constants instead.

* http://search.cpan.org/dist/Readonly/Readonly.pm


Thanks. Hmm, they say it is slow 
(http://search.cpan.org/dist/Readonly/Readonly.pm#CONS).


Why aren't there constants in the language itself?? This is crazy.

Also: is that true that Perl's specification is its implementation by 
Larry Wall? This is even more crazy. :-/

A language and its implementation are (should be) 2 different things!

Gunnar Hjalmarsson wrote:

So, what behavior exactly is it that you consider weird? What output had you 
expected?


I'd expect equal hash keys to map to equal values.
I'm a conservatist. :-)

STF

===
http://eisenbits.homelinux.net/~stf/ . My PGP key fingerprint is:
9D25 3D89 75F1 DF1D F434  25D7 E87F A1B9 B80F 8062
===



signature.asc
Description: OpenPGP digital signature


Re: hashes + use constant - weird behavior

2009-03-05 Thread Gunnar Hjalmarsson

Stanisław T. Findeisen wrote:

#!/usr/bin/perl

use warnings;
use strict;

use constant {
SOME_CONSTANT => 'some value'
};

my $index = 'some value';
my %hash = ();

$hash{SOME_CONSTANT} = 'value 1';
$hash{$index} = 'value 2';

print("The value is: " . $hash{SOME_CONSTANT} . '/' . $hash{$index} . 
"\n");

print("Comparison 1: " .   (SOME_CONSTANT  eq $index) . "\n");
print("Comparison 2: " . ($hash{SOME_CONSTANT} ne $hash{$index}) . "\n");

$ perl ./hash-test.pl
The value is: value 1/value 2
Comparison 1: 1
Comparison 2: 1

???


So, what behavior exactly is it that you consider weird? What output had 
you expected?


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: hashes + use constant - weird behavior

2009-03-05 Thread Chas. Owens
On Thu, Mar 5, 2009 at 12:23, "Stanisław T. Findeisen"
 wrote:
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> use constant {
>    SOME_CONSTANT => 'some value'
> };
>
snip
> print("The value is: " . $hash{SOME_CONSTANT} . '/' . $hash{$index} . "\n");
snip

SOME_CONSTANT is being interpreted as the string "SOME_CONSTANT".
This is one of the drawbacks to the constant pragma.  Change the code
hash keys to one of these and it will work the way you want it to:

$hash{+SOME_CONSTANT} #unary plus
$hash{&SOME_CONSTANT} #function call method 1
$hash{SOME_CONSTANT()} #function call method 2

However, I would suggest using the ReadOnly* module for constants instead.

* http://search.cpan.org/dist/Readonly/Readonly.pm

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




hashes + use constant - weird behavior

2009-03-05 Thread Stanisław T. Findeisen

#!/usr/bin/perl

use warnings;
use strict;

use constant {
SOME_CONSTANT => 'some value'
};

my $index = 'some value';
my %hash = ();

$hash{SOME_CONSTANT} = 'value 1';
$hash{$index} = 'value 2';

print("The value is: " . $hash{SOME_CONSTANT} . '/' . $hash{$index} . "\n");
print("Comparison 1: " .   (SOME_CONSTANT  eq $index) . "\n");
print("Comparison 2: " . ($hash{SOME_CONSTANT} ne $hash{$index}) . "\n");

$ perl ./hash-test.pl
The value is: value 1/value 2
Comparison 1: 1
Comparison 2: 1

???

$ perl -v

This is perl, v5.8.8 built for i386-linux-thread-multi

[...]

STF

===
http://eisenbits.homelinux.net/~stf/ . My PGP key fingerprint is:
9D25 3D89 75F1 DF1D F434  25D7 E87F A1B9 B80F 8062
===



signature.asc
Description: OpenPGP digital signature


Re: "use constant" as hash key

2008-08-25 Thread Xavier Mas
El Monday 25 August 2008 14:30:14 Moon, John va escriure:
> > How can I use a "constant" as a hash key?
> >
> > $ perl -e 'use constant CAT=>A;
> >
> >> $hash{CAT} = q{Bobby};
> >> $hash{"CAT"} = q{Muffy};
> >> $hash{'CAT'} = q{Fluffy};
> >> $hash{qq{CAT}} = q{Tuffy};
> >> print "$_ = $hash{$_}\n" foreach (keys %hash);'
> >
> > CAT = Tuffy
> > $
> >
> > Want...
> >
> > A=Bobby
>
> See the "Not-so-symbolic references" section of the perlref.pod man page
>
> for some hints:
>
> perldoc perlref
>
>
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order.-- Larry Wall
>
>
> [>>] Thank you...
>
> >>perl -we 'use constant CAT=>a; $hash{+CAT} = q{Bobby}; print "$_ =
>
> $hash{$_}\n" foreach (keys %hash);'
>
> jwm

key must be always unique.

-- 
Xavier Mas


__ 
LLama Gratis a cualquier PC del Mundo. 
Llamadas a fijos y móviles desde 1 céntimo por minuto. 
http://es.voice.yahoo.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: "use constant" as hash key

2008-08-25 Thread Moon, John
> How can I use a "constant" as a hash key?
> 
> $ perl -e 'use constant CAT=>A;
>> $hash{CAT} = q{Bobby};
>> $hash{"CAT"} = q{Muffy};
>> $hash{'CAT'} = q{Fluffy};
>> $hash{qq{CAT}} = q{Tuffy};
>> print "$_ = $hash{$_}\n" foreach (keys %hash);'
> CAT = Tuffy
> $
> 
> Want...
> 
> A=Bobby

See the "Not-so-symbolic references" section of the perlref.pod man page

for some hints:

perldoc perlref



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall


[>>] Thank you... 

>>perl -we 'use constant CAT=>a; $hash{+CAT} = q{Bobby}; print "$_ =
$hash{$_}\n" foreach (keys %hash);'

jwm

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: "use constant" as hash key

2008-08-23 Thread Peter Scott
On Thu, 21 Aug 2008 14:15:57 -0400, Moon, John wrote:
> How can I use a "constant" as a hash key?

Having to know the answer to this is one of the reasons that 'use
constant' is considered an inferior practice compared to

use Readonly;
Readonly my $CAT => 'A';

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: "use constant" as hash key

2008-08-23 Thread Dr.Ruud
"Moon, John" schreef:

> How can I use a "constant" as a hash key?

#!/usr/bin/perl
  use strict;
  use warnings;
  use Data::Dumper;

  use constant FOO => 'bar';

  my %hash;

  $hash{  FOO   } = 'one';
  $hash{ +FOO   } = 'two';
  $hash{ 'FOO'  } = 'three';
  $hash{ -FOO   } = 'four';
  $hash{  FOO() } = 'five';
  print Dumper( \%hash );

  %hash = (
  FOO   => 1,
 +FOO   => 2,
 'FOO'  => 3,
 -FOO   => 4,
  FOO() => 5,
  );
  print Dumper( \%hash );
__END__


-- 
Affijn, Ruud

"Gewoon is een tijger."

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: "use constant" as hash key

2008-08-22 Thread John W. Krahn

Rob Dixon wrote:

Moon, John wrote:

How can I use a "constant" as a hash key?

$ perl -e 'use constant CAT=>A;

$hash{CAT} = q{Bobby};
$hash{"CAT"} = q{Muffy};
$hash{'CAT'} = q{Fluffy};
$hash{qq{CAT}} = q{Tuffy};
print "$_ = $hash{$_}\n" foreach (keys %hash);'

CAT = Tuffy
$

Want...

A=Bobby


If you remember that constants are implemented as subroutines then all you have
to do is write the hash key as an expression that forces the subroutine to be
called. So

  use constant CAT => 'A';

is equivalent to

  sub CAT { 'A' }


It is actually equivalent to:

   sub CAT () { 'A' }

See the "Constant Functions" section of perlsub.pod

perldoc perlsub



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: "use constant" as hash key

2008-08-22 Thread Rob Dixon
Moon, John wrote:
>
> How can I use a "constant" as a hash key?
> 
> $ perl -e 'use constant CAT=>A;
>> $hash{CAT} = q{Bobby};
>> $hash{"CAT"} = q{Muffy};
>> $hash{'CAT'} = q{Fluffy};
>> $hash{qq{CAT}} = q{Tuffy};
>> print "$_ = $hash{$_}\n" foreach (keys %hash);'
> CAT = Tuffy
> $
> 
> Want...
> 
> A=Bobby

If you remember that constants are implemented as subroutines then all you have
to do is write the hash key as an expression that forces the subroutine to be
called. So

  use constant CAT => 'A';

is equivalent to

  sub CAT { 'A' }

and you can access the hash element with an expression like

  $hash{CAT()} = 'Daffy';
  $hash{CAT} = 'Duffy';
  $hash{CAT.''} = 'Dippy';
  $hash{0 || CAT} = 'Depot';

HTH,

Rob


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: "use constant" as hash key

2008-08-22 Thread John W. Krahn

Moon, John wrote:

How can I use a "constant" as a hash key?

$ perl -e 'use constant CAT=>A;

$hash{CAT} = q{Bobby};
$hash{"CAT"} = q{Muffy};
$hash{'CAT'} = q{Fluffy};
$hash{qq{CAT}} = q{Tuffy};
print "$_ = $hash{$_}\n" foreach (keys %hash);'

CAT = Tuffy
$

Want...

A=Bobby


See the "Not-so-symbolic references" section of the perlref.pod man page 
for some hints:


perldoc perlref



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




"use constant" as hash key

2008-08-22 Thread Moon, John
How can I use a "constant" as a hash key?

$ perl -e 'use constant CAT=>A;
> $hash{CAT} = q{Bobby};
> $hash{"CAT"} = q{Muffy};
> $hash{'CAT'} = q{Fluffy};
> $hash{qq{CAT}} = q{Tuffy};
> print "$_ = $hash{$_}\n" foreach (keys %hash);'
CAT = Tuffy
$

Want...

A=Bobby

John W Moon


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: use constant

2005-12-20 Thread JupiterHost.Net



David Gilden wrote:
Greetings, 


I looked for documentation on 'use constant'  but it has eluded it me.


perldoc constant


my $path =  `pwd`;
use constant UPLOAD_DIR => "/home/sites/site123/web/private/_data/";


I want to get the Document Root at runtime and concatenate like such...


my $path =  `pwd`;
use constant UPLOAD_DIR => "$path/_data/";



use Cwd; for that, one reason (out of many) is that now UPLOAD_DIR is 
actually:


/i/am/here
/_data/

because of the newline in the pwd backtick command


But this fails.

is this construct from the PERL 4 days?
constant UPLOAD_DIR

Any guidance would be greatly appreciated.


You can also use ReadOnly; and even better is to use File::Spec to join 
what Cwd gives you to _data, more portable...


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: use constant

2005-12-20 Thread DBSMITH
   
 "John W. Krahn"   
 <[EMAIL PROTECTED] 
 >  To 
   Perl Beginners  
 12/19/2005 08:29   cc 
 PM
   Subject 
       Re: use constant
   
   
   
   
   
   







David Gilden wrote:
> Greetings,

Hello,

> I looked for documentation on 'use constant'  but it has eluded it
me.....
>
> my $path =  `pwd`;
> use constant UPLOAD_DIR => "/home/sites/site123/web/private/_data/";
>
>
> I want to get the Document Root at runtime and concatenate like such...
>
>
> my $path =  `pwd`;

You should probably use the Cwd module to do that.

> use constant UPLOAD_DIR => "$path/_data/";

use Cwd;

my $path;
BEGIN { $path = cwd }

use constant UPLOAD_DIR => "$path/_data/";



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



****

David,
although there are various ways to do what John stated, the use constant is
not a recomended
pragma according to the Oreilly book "Perl Best Practices."
Rather Readonly is a better method b/c it can be interpolated.

use Readonly;
my $SCALER => 'value';

use Readonly;
my @ARRAY => 'value';

use Readonly;
 my %HASH => 'value';

for versions 5.8 and above

but for versions 5.6 and below

use Readonly::Scaler;
 my $SCALER => 'value';



Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: use constant

2005-12-20 Thread Shawn Corey

David Gilden wrote:

I looked for documentation on 'use constant'  but it has eluded it me.


perldoc constant


--

Just my 0.0002 million dollars worth,
   --- Shawn

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is available at http://perldoc.perl.org/

* Always write your code as though you aren't going to see it
* for another 25 years and then one day your boss comes up to you
* and says, "I want these changes and I want them yesterday!"

"Probability is now one. Any problems that are left are your own."
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: use constant

2005-12-19 Thread John W. Krahn
David Gilden wrote:
> Greetings, 

Hello,

> I looked for documentation on 'use constant'  but it has eluded it me.
> 
> my $path =  `pwd`;
> use constant UPLOAD_DIR => "/home/sites/site123/web/private/_data/";
> 
> 
> I want to get the Document Root at runtime and concatenate like such...
> 
> 
> my $path =  `pwd`;

You should probably use the Cwd module to do that.

> use constant UPLOAD_DIR => "$path/_data/";

use Cwd;

my $path;
BEGIN { $path = cwd }

use constant UPLOAD_DIR => "$path/_data/";



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




use constant

2005-12-19 Thread David Gilden
Greetings,

I looked for documentation on 'use constant'  but it has eluded it me.

my $path =  `pwd`;
use constant UPLOAD_DIR => "/home/sites/site123/web/private/_data/";


I want to get the Document Root at runtime and concatenate like such...


my $path =  `pwd`;
use constant UPLOAD_DIR => "$path/_data/";

But this fails.

is this construct from the PERL 4 days?
constant UPLOAD_DIR

Any guidance would be greatly appreciated.

Thx,

Dave Gilden 
(kora musician / audiophile / webmaster @ www.coraconnection.com  / Ft. Worth, 
TX, USA)

Cool music.From Guineé comes this CD of kora fusion, electronica  
[ Audition Mp3s at the URL below ]
<http://www.coraconnection.com/pages/other_cds.html#sabolan>






--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: can anyone help resolve compilation error related to use constant

2005-06-27 Thread Wiggins d'Anconia
MEENA SELVAM wrote:
> I have a section of code from a .pl file as follows:
> 
> 
> package SF::Constants;
> use Exporter;
> 
> @ISA = qw(Exporter);
> @EXPORT = qw(SF_SUCCESS SF_EINVAL SF_ENOSYS SF_ENOMEM
> SF_ERANGE SF_EPERM SF_ENOENT SF_EEXIST SF_EDATABASE
> SF_ESYNTAX SF_NOUSER SF_NOUSERROLE SF_NOTIMESPENT
> SF_NOCOMMENT SF_ETRANSACT SF_NOTYPE SF_NOSTATE
> SF_NOSUMMARY SF_EBUSY SF_ENOSPC SF_EREAD
> SF_END_OF_FILE SF_EAGAIN SF_EREAD_PARTIAL SF_ENOTCONN
> SF_EREAD_TRUNCATED SF_CLOSED SF_ENOPROTOSUPPORT
> SF_ENOSUPPORT SF_EWRITE SF_EWRITE_PARTIAL SF_EBADLEN
> SF_EPROTOCOL_VIOL SF_EPEER SF_ENOTDIR SF_EMUTEX
> SF_EMUTEX_INVAL SF_EMUTEX_DEADLK SF_EOPEN SF_ELOCKED
> SF_ESSL SF_ELICENSE_INVAL SF_ELICENSE_PLATFORM
> SF_ELICENSE_CORRUPT SF_ESSL_NOCIPHERS
> SF_ESSL_CRLEXPIRED SF_ENOMATCH SF_ESOCKET SF_ENITRO
> SF_ENOLICENSE SF_EHASLICENSE SF_ECORRUPT SF_EBAD_MAGIC
> SF_EBAD_LINKTYPE SF_NITRO_DUPLICATE  SF_MAX_ERRNUM); 
>   ### Return Codes ###
> 
> 
> use constant SF_SUCCESS       => 0  # Success
> use constant SF_EINVAL    => 1  # Invalidargument
> use constant SF_ENOSYS=> 2  # Unimplemented
> use constant SF_ENOMEM=> 3  # Out of memory
> use constant SF_ERANGE=> 4  # Out of range

The above statements *as is* do not have semi-colons. Try adding them
and see if that resolves your issue. You also may want to read,

perldoc constant

Particularly the section on defining multiple constants at once. It can
reduce the typing, and *maybe* be more readable. I have used both ways
and don't have a strong feeling in either direction.

HTH,

http://danconia.org

> 
> I get the following compilation error:
> --
> use not allowed in expression at
> /usr/local/sf/lib/perl/5.8.3/SF/SFError.pm at line 10
> end of line
> 
> syntax error at line 10 - near use constant
> 
> compilation failed in require at ../snas.pl line 16
> 
> -
> line 10 is the "use constant SFEINVAL" which seems
> right for me
> 
> Actually snas.pl has the following statements
> use lib "/usr/local/sf/lib/perl/5.8.3/SF"
> use SFError qw(:try);
> 
> --
> 
> Does anyone see anything wrong in the syntax or reason
> for this compilation error

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: can anyone help resolve compilation error related to use constant

2005-06-27 Thread MEENA SELVAM
I have a section of code from a .pl file as follows:


package SF::Constants;
use Exporter;

@ISA = qw(Exporter);
@EXPORT = qw(SF_SUCCESS SF_EINVAL SF_ENOSYS SF_ENOMEM
SF_ERANGE SF_EPERM SF_ENOENT SF_EEXIST SF_EDATABASE
SF_ESYNTAX SF_NOUSER SF_NOUSERROLE SF_NOTIMESPENT
SF_NOCOMMENT SF_ETRANSACT SF_NOTYPE SF_NOSTATE
SF_NOSUMMARY SF_EBUSY SF_ENOSPC SF_EREAD
SF_END_OF_FILE SF_EAGAIN SF_EREAD_PARTIAL SF_ENOTCONN
SF_EREAD_TRUNCATED SF_CLOSED SF_ENOPROTOSUPPORT
SF_ENOSUPPORT SF_EWRITE SF_EWRITE_PARTIAL SF_EBADLEN
SF_EPROTOCOL_VIOL SF_EPEER SF_ENOTDIR SF_EMUTEX
SF_EMUTEX_INVAL SF_EMUTEX_DEADLK SF_EOPEN SF_ELOCKED
SF_ESSL SF_ELICENSE_INVAL SF_ELICENSE_PLATFORM
SF_ELICENSE_CORRUPT SF_ESSL_NOCIPHERS
SF_ESSL_CRLEXPIRED SF_ENOMATCH SF_ESOCKET SF_ENITRO
SF_ENOLICENSE SF_EHASLICENSE SF_ECORRUPT SF_EBAD_MAGIC
SF_EBAD_LINKTYPE SF_NITRO_DUPLICATE  SF_MAX_ERRNUM); 
  ### Return Codes ###


use constant SF_SUCCESS => 0  # Success
use constant SF_EINVAL  => 1  # Invalidargument
use constant SF_ENOSYS  => 2  # Unimplemented
use constant SF_ENOMEM  => 3  # Out of memory
use constant SF_ERANGE  => 4  # Out of range

I get the following compilation error:
--
use not allowed in expression at
/usr/local/sf/lib/perl/5.8.3/SF/SFError.pm at line 10
end of line

syntax error at line 10 - near use constant

compilation failed in require at ../snas.pl line 16

-
line 10 is the "use constant SFEINVAL" which seems
right for me

Actually snas.pl has the following statements
use lib "/usr/local/sf/lib/perl/5.8.3/SF"
use SFError qw(:try);

--

Does anyone see anything wrong in the syntax or reason
for this compilation error
--- Wiggins d'Anconia <[EMAIL PROTECTED]> wrote:

> MEENA SELVAM wrote:
> > My perl program snas.pl uses SF:Logger.pm which is
> > available in /usr/local/sf/lib/perl/5.8.3/SF. I
> have
> > added this directory in the path (.cshrc and
> .tshrc).
> > I am running as root, and I get the cant locate
> > SF::Logger.pm error in $INC.
> > 
> > $INC contains usr/lib/perl5/5.6.1/i386_linux and
> some
> > more usr/lib/perl5/... directories
> > 
> > How can i alter this $INC. I tried using 
> > use lib "/usr/local/sf/lib/perl/5.8.3/SF"; since I
> > found that this should work as per perl.apache.org
> > perl reference document.
> > 
> > meena
> > 
> 
> If the module you are using is SF::Logger then the
> path you need to add
> is like the above only without the 'SF', perl will
> add that
> automagically. Try,
> 
> use lib "/usr/local/sf/lib/perl/5.8.3";
> 
> Alternatively, Perl doesn't use the $PATH shell
> environment variable, it
> used $PERL5LIB instead. In bash for instance,
> 
> export
> PERL5LIB="$PERL5LIB:/usr/local/sf/lib/perl/5.8.3"
> 
> Should do the trick
> 
> http://danconia.org
> 




 
Yahoo! Sports 
Rekindle the Rivalries. Sign up for Fantasy Football 
http://football.fantasysports.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: erorr with "use constant"

2004-04-27 Thread Wiggins d Anconia
Please bottom post...

> 
> why not use & for a function call?  Aren't & and ( ) the same?
> I used local originally  b/c I wanted to use this globally from within
the 
> function.  Is there a preferred way to use local?
> 

& and () may not be the same, see

perldoc -q calling

I am not sure what you mean by "use this globally from within the
function".  Remember that we only see a small portion of your code, from
what I have seen there is no reason to use local in this context.
'local' just tells Perl to restore the value of the variable after you
leave the current scope, which didn't seem to be needed in this context. 

An excellent discussion of scoping can be found here:

http://perl.plover.com/FAQs/Namespaces.html

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: erorr with "use constant"

2004-04-27 Thread DBSMITH
why not use & for a function call?  Aren't & and ( ) the same?
I used local originally  b/c I wanted to use this globally from within the 
function.  Is there a preferred way to use local?

thanks 


Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
614-566-4145





"Wiggins d Anconia" <[EMAIL PROTECTED]>
04/27/2004 03:06 PM

 
To: [EMAIL PROTECTED], [EMAIL PROTECTED]
cc: 
Subject:    Re: erorr with "use constant"




> 
> Can any provide some help?
> I am getting this error: 
> 
> edm01:/usr/local/bin/perl>> perl -c tape_rotation_vaulting.pl
> String found where operator expected at tape_rotation_vaulting.pl line
64, 
> near"
> (Do you need to predeclare TWENTYONEDAYS_STRING?)
> syntax error at tape_rotation_vaulting.pl line 64, near 
> "TWENTYONEDAYS_STRING ""
> tape_rotation_vaulting.pl had compilation errors.
> 

I think this is misleading you and you actually need an operator.

> 
> I want to be able to print the string "There are x seconds in 21 
days"
>  would come from the constant TWENTYONEDAYS_STRING.
> 
> thanks!
> 
> ## Set pragmas
> 
> # use strict;
> # use diagnostics;
> 

Commenting out the above prevents them from helping you... The first
should never need to be commented.

> ## Set variables
> 
> #my $twenty_one_days="/usr/local/log/21days.log";
> #my $thiry_days="/usr/local/log/30days.log";
> #my $ninety_one_days="/usr/local/log/91days.log";
> my $default="/usr/local/log/edmdefault.log";
> my $twenty_one_days_LnotesFS="/usr/local/log/LnotesFS.log";
> my $lvimg_L_Drive="/usr/local/log/lvimg_L_Drive.log";
> my $lvimg_K_Drive="/usr/local/log/lvimg_K_Drive.log";
> my $lvimg_JI_Drives="/usr/local/log/lvimg_JI_Drives.log";
> my $lvimg_GH_Drives="/usr/local/log/lvimg_GH_Drives.log";
> my $lvimg_FE_Drives="/usr/local/log/lvimg_FE_Drives.log";
> my $lvimg_DC_Drives="/usr/local/log/lvimg_DC_Drives.log";
> 
> ## Below are SC clients
> 
> #my $idxsql="/usr/local/log/idxsql.log";
> #my $ORBaplog="/usr/local/log/ORBaplog.log";
> #my $ORBDsch="/usr/local/log/ORBDsch.log";
> #my $ORBmodel="/usr/local/log/ORBmodel.log";
> #my $ORBmsdb="/usr/local/log/ORBmsdb.log";
> #my $ORBOHusr="/usr/local/log/ORBOHusr.log";
> #my $ORBPo="/usr/local/log/ORBPo.log";
> #my $ORBPr="/usr/local/log/ORBPr.log";
> #my $ORB="/usr/local/log/ORB.log";
> 
> 
>  system ("clear");

Know why this may or may not work...

> 
> ## Epoch time == 21,30 and 91
> 
> ## Define global constants
> 
>  use constant TWENTYONEDAYS_STRING => 60 * 60 * 24 * 21;
>  use constant THIRTYDAYS_STRING => 60 * 60 * 24 * 30;
>  use constant NINETYONEDAYS_STRING => 60 * 60 * 24 * 91;
> 
> # use constant THIRTYDAYS_STRING => localtime(time + 2592000);
> # use constant NINETYONEDAYS_STRING => localtime(time + 7862400);
> 
> 
> &timing;
> 

Drop the ampersand until you know why you need it.

timing();

Is preferred.

> sub timing {
>   local $now_string = localtime;

Use lexicals instead of locals until you know why you shouldn't.

my $now_string = localtime;

>   print $now_string, "\n";
>   print "\n";
>   printf "There are ", TWENTYONEDAYS_STRING "seconds in 21 days"; 

perldoc -f printf

printf takes a format followed by optional parameters to be dropped into
that format, you don't need it here.

print "There are " . TWENTYONEDAYS_STRING . "seconds in 21 days";

Should work. I believe your error is caused because you are missing a
comma after the constant.

HTH,

http://danconia.org




Re: erorr with "use constant"

2004-04-27 Thread Wiggins d Anconia


> 
> Can any provide some help?
> I am getting this error: 
> 
> edm01:/usr/local/bin/perl>> perl -c tape_rotation_vaulting.pl
> String found where operator expected at tape_rotation_vaulting.pl line
64, 
> near"
> (Do you need to predeclare TWENTYONEDAYS_STRING?)
> syntax error at tape_rotation_vaulting.pl line 64, near 
> "TWENTYONEDAYS_STRING ""
> tape_rotation_vaulting.pl had compilation errors.
> 

I think this is misleading you and you actually need an operator.

> 
> I want to be able to print the string "There are x seconds in 21 days"
>  would come from the constant TWENTYONEDAYS_STRING.
> 
> thanks!
> 
> ## Set pragmas
> 
> # use strict;
> # use diagnostics;
> 

Commenting out the above prevents them from helping you... The first
should never need to be commented.

> ## Set variables
> 
> #my $twenty_one_days="/usr/local/log/21days.log";
> #my $thiry_days="/usr/local/log/30days.log";
> #my $ninety_one_days="/usr/local/log/91days.log";
> my $default="/usr/local/log/edmdefault.log";
> my $twenty_one_days_LnotesFS="/usr/local/log/LnotesFS.log";
> my $lvimg_L_Drive="/usr/local/log/lvimg_L_Drive.log";
> my $lvimg_K_Drive="/usr/local/log/lvimg_K_Drive.log";
> my $lvimg_JI_Drives="/usr/local/log/lvimg_JI_Drives.log";
> my $lvimg_GH_Drives="/usr/local/log/lvimg_GH_Drives.log";
> my $lvimg_FE_Drives="/usr/local/log/lvimg_FE_Drives.log";
> my $lvimg_DC_Drives="/usr/local/log/lvimg_DC_Drives.log";
> 
> ## Below are SC clients
> 
> #my $idxsql="/usr/local/log/idxsql.log";
> #my $ORBaplog="/usr/local/log/ORBaplog.log";
> #my $ORBDsch="/usr/local/log/ORBDsch.log";
> #my $ORBmodel="/usr/local/log/ORBmodel.log";
> #my $ORBmsdb="/usr/local/log/ORBmsdb.log";
>     #my $ORBOHusr="/usr/local/log/ORBOHusr.log";
> #my $ORBPo="/usr/local/log/ORBPo.log";
>     #my $ORBPr="/usr/local/log/ORBPr.log";
> #my $ORB="/usr/local/log/ORB.log";
> 
> 
>  system ("clear");

Know why this may or may not work...

> 
> ## Epoch time == 21,30 and 91
> 
> ## Define global constants
> 
>  use constant TWENTYONEDAYS_STRING => 60 * 60 * 24 * 21;
>  use constant THIRTYDAYS_STRING => 60 * 60 * 24 * 30;
>  use constant NINETYONEDAYS_STRING => 60 * 60 * 24 * 91;
>  
> # use constant THIRTYDAYS_STRING => localtime(time + 2592000);
> # use constant NINETYONEDAYS_STRING => localtime(time + 7862400);
> 
> 
> &timing;
> 

Drop the ampersand until you know why you need it.

timing();

Is preferred.

> sub timing {
>   local $now_string = localtime;

Use lexicals instead of locals until you know why you shouldn't.

my $now_string = localtime;

>   print $now_string, "\n";
>   print "\n";
>   printf "There are ", TWENTYONEDAYS_STRING "seconds in 21 days";  

perldoc -f printf

printf takes a format followed by optional parameters to be dropped into
that format, you don't need it here.

print "There are " . TWENTYONEDAYS_STRING . "seconds in 21 days";

Should work. I believe your error is caused because you are missing a
comma after the constant.

HTH,

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




erorr with "use constant"

2004-04-27 Thread DBSMITH
Can any provide some help?
I am getting this error: 

edm01:/usr/local/bin/perl>> perl -c tape_rotation_vaulting.pl
String found where operator expected at tape_rotation_vaulting.pl line 64, 
near"
(Do you need to predeclare TWENTYONEDAYS_STRING?)
syntax error at tape_rotation_vaulting.pl line 64, near 
"TWENTYONEDAYS_STRING ""
tape_rotation_vaulting.pl had compilation errors.


I want to be able to print the string "There are x seconds in 21 days"
 would come from the constant TWENTYONEDAYS_STRING.

thanks!

## Set pragmas

# use strict;
# use diagnostics;

## Set variables

#my $twenty_one_days="/usr/local/log/21days.log";
#my $thiry_days="/usr/local/log/30days.log";
#my $ninety_one_days="/usr/local/log/91days.log";
my $default="/usr/local/log/edmdefault.log";
my $twenty_one_days_LnotesFS="/usr/local/log/LnotesFS.log";
my $lvimg_L_Drive="/usr/local/log/lvimg_L_Drive.log";
my $lvimg_K_Drive="/usr/local/log/lvimg_K_Drive.log";
my $lvimg_JI_Drives="/usr/local/log/lvimg_JI_Drives.log";
my $lvimg_GH_Drives="/usr/local/log/lvimg_GH_Drives.log";
my $lvimg_FE_Drives="/usr/local/log/lvimg_FE_Drives.log";
my $lvimg_DC_Drives="/usr/local/log/lvimg_DC_Drives.log";

## Below are SC clients

#my $idxsql="/usr/local/log/idxsql.log";
#my $ORBaplog="/usr/local/log/ORBaplog.log";
#my $ORBDsch="/usr/local/log/ORBDsch.log";
#my $ORBmodel="/usr/local/log/ORBmodel.log";
#my $ORBmsdb="/usr/local/log/ORBmsdb.log";
#my $ORBOHusr="/usr/local/log/ORBOHusr.log";
#my $ORBPo="/usr/local/log/ORBPo.log";
#my $ORBPr="/usr/local/log/ORBPr.log";
#my $ORB="/usr/local/log/ORB.log";


 system ("clear");

## Epoch time == 21,30 and 91

## Define global constants

 use constant TWENTYONEDAYS_STRING => 60 * 60 * 24 * 21;
 use constant THIRTYDAYS_STRING => 60 * 60 * 24 * 30;
 use constant NINETYONEDAYS_STRING => 60 * 60 * 24 * 91;
 
# use constant THIRTYDAYS_STRING => localtime(time + 2592000);
# use constant NINETYONEDAYS_STRING => localtime(time + 7862400);


&timing;

sub timing {
  local $now_string = localtime;
  print $now_string, "\n";
  print "\n";
  printf "There are ", TWENTYONEDAYS_STRING "seconds in 21 days";  print 
"\n";
} 

Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams