Useless use of a (constant|hash element) in void context?

Jacinta Richardson jarich at perltraining.com.au
Sun Oct 5 20:31:53 CDT 2003


On Mon, 6 Oct 2003, ADFH wrote:

> > Instead, you need here to assign a hashref, which is the only way to 
> > assign a hash to a scalar.
> 
> So how does one get a reference out of a hash constructor - case of two
> line process?
> 
> Ie.
> 	my %temphash=( .... => .... , .... );
> 	$dbct{$dbr_ref->{'field'}}=\%temphash;

This can be replaced with:
	$dbct{$dbr_ref->{'field'}}= { ... => ...,
				      ... => .... };

Note the curlies.  An even length list in curly braces denotes a
(annonymous) hash reference.  A list in square brackets [] denotes an
(annonymous) array reference. You mentioned that you attended a Netizen
course in the past.  You may be interested in knowing that Paul and I have
been further developing those notes and you can see our coverage of
references and the like in our Intermediate course notes at: 

	http://www.perltraining.com.au

Annonymous references are called such because they have no name, whereas
in your example above your hash does have a name.

> I guess I've always been curious - what's the difference between
> prefixing something with an ampersand or a backslash? I'm thinking
> ampersand dereferences and backslash generates reference.

If you prefix something with an ampersand you're saying that it's a
subroutine:

	&some_sub();

that you wish to call.  If you wish to take a subroutine reference you can
do it as follows:

	my $sub_ref = \&some_sub;

or
	my $sub_ref = sub { ..... };  (annonymous subroutine).

> Is an area I never quite got (once I figure out this stuff, I think I'll
> revisit scoping in Perl then OO then making modules - experience is
> beginning to tell me, that just like learning vi :) , things tend to
> just "click"  after awhile).. 

All the best at that.  I hope our notes may be of use to you.

	Jacinta

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001         |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |




More information about the Melbourne-pm mailing list