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

Re: Constants and Hash Question

Thread Previous | Thread Next
From:
John W. Krahn
Date:
December 11, 2002 15:14
Subject:
Re: Constants and Hash Question
Message ID:
3DF7C6AC.D1B2D4CA@acm.org
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
subroutine.

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



John
-- 
use Perl;
program
fulfillment

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About