In C when you do a malloc(), you are getting a hunk of memory and assigning that address to a variable which points to the memory. Pretty much the same concept in Perl... my $hashref={}; e.g. allocate some memory and assign a reference (pointer) to it to $hashref. There is a kind of weird duality between arrays and hash tables and () would work here. But in general, {} is used for a new anon hash table memory allocation, and [] means a new array memory allocation.

In Perl, there is no need to "define" how you are going to use some memory, you just start using it. A wild concept for a C programmer, but this is true. If some hash key doesn't already exist, like below (username), it will spring automagically into existence! This is called autovivification.

Like in C you can pass a reference (like a pointer) to a sub as is done below. There is no need to prefix a subroutine call with &. You have been reading some truly ancient books! You should buy a new copy of "Programming Perl". There is no need and this is actually a bad idea to attempt to define prototypes for a function (subroutine). Below I call test() before it is "seen by the compiler", this is fine in Perl although that would cause big trouble in C. Of course I would not normally split code around a subroutine, but below it is done to show that it is possible. You can put sub test() anywhere in the file you want.

#!/usr/bin/perl -w # the above (-w) turns on warnings, Windows ignores the path!! use strict; use Data::Dumper; my $hashref={}; #a reference to anon hash table $hashref->{username} = 'NOOoooooo!'; print "User Name: $hashref->{username}\n"; test( $hashref ); sub test { my $reference = shift; my $test = "Pigsy's Perfect Ten"; $reference->{username} = $test; print "Inside Sub Test test\'s value is: $reference->{username}\n" +; $reference->{'accountid'} = '234piggy'; } print "User Name: $hashref->{username}\n"; print "Account ID: $hashref->{'accountid'}\n"; __END__ Prints: User Name: NOOoooooo! Inside Sub Test test's value is: Pigsy's Perfect Ten User Name: Pigsy's Perfect Ten Account ID: 234piggy

In reply to Re^4: Elaborate Records, arrays and references by Marshall
in thread Elaborate Records, arrays and references by KyussRyn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.