develooper Front page | perl.beginners | Postings from December 2002

Re: Constants and Hash Question

Thread Previous | Thread Next
John W. Krahn
December 11, 2002 15:14
Re: Constants and Hash Question
Message ID:
Paul Kraus wrote:
> I am declaring my constants like this.
> use constant SOURCE => "/path/folder/"
> Now how would I use this constant is a print statement or any statement
> for that matter.
> print "$SOURCE" does not work and of course print "source" will not
> work.

The constant pragma creates a subroutine so that:

use constant SOURCE => "/path/folder/"

Is equivalent to:

sub SOURCE () { "/path/folder/" }

So you can use a constant created this way the same as you would use a

print SOURCE, "file.txt\n";
print SOURCE . "file.txt\n";
print "@{[SOURCE]}file.txt\n";

> I understand that this works but I do not understand what its doing and
> the need for the parenthesis.
> Could someone explain this statement for me. I have put <> around the
> part I don't understand.
> this is part of a tied hash for Config::IniFiles.
> --=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> while (($k,$v) = each <%{$ini{$section}}>)

The value of $ini{$section} is a reference to a hash so you have to put
%{} around it to dereference the hash.

perldoc perlreftut
perldoc perlref

use Perl;

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About