1

I have looked at this SO post, and saw the part about

my @array = ('a', 'b', 'c');
my  $variable  = @array;           #  3   size of @array
my ($variable) = @array;           # 'a'  $array[0]

assigning the first element of @array to my ($variable).

I am a little confused about the following code that I have inherited. Specifically, is

my ($ptBatchRec);

being declared as a pointer?

What would happen if I removed the parentheses?

my $DBHandle = shift;                   # The ICS database handle
my $ptBatchStruct = shift;              # -> batch structure (hash)
my ($FiscalYear, $AccountType, $BatchType, $PaymentType, %Args) = @_;

my $DBIsLocked = 0;                     # Database is locked flag
my ($ptBatchRec);                       # -> batch record
.
.
.
    $ptBatchRec = $DBHandle->selectrow_hashref(
        "select * from batch " .
            "where yr matches ".$DBHandle->quote($FiscalYear)." " .
                "and entry_date = " .
                    $DBHandle->quote($ptBatchStruct->{"entry_date"})." " .
                "and acct_type = ".$DBHandle->quote($AccountType)." " .
                "and batch_type = ".$DBHandle->quote($BatchType)." " .
                "and pymt_code = ".$DBHandle->quote($PaymentType)." " .
            "order by batch_num desc");
Community
  • 1
  • 1
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
  • Legibility would benefit from query placeholders: `$db->selectrow_hashref("select * from batch where yr matches ? and entry_date = ? ...", undef, $FiscalYear, $ptB->{entry_date}, ...)` – pilcrow Jun 27 '13 at 18:38

2 Answers2

5

It's not being declared as anything in particular (it's a scalar, from the $, and it doesn't get initialized so it will start out holding undef).

Then it's assigned the result of selectrow_hashref, which you can tell from the name, will return a hash reference (at least if it's successful; it could be undef on failure). A reference is a little bit like a pointer, but there are no "pointers" in perl. perlref and perlreftut are good resources here.

In particular, using parentheses does not change the declaration.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
hobbs
  • 223,387
  • 19
  • 210
  • 288
3
  1. Perl has no pointers. They are called references (but are exactly like pointers minus pointer arithmetic).

  2. The parens are no special syntax. Here, they just supply list context for RHS of the assignment. I.e.

    my ($x, $y) = @array;
    

    is the same as

    (my $x, my $y) = ($array[0], $array[1]);
    

    is the same as

    my $x = $array[0]; my $y = $array[1];
    

    Thus the special case of one element on the LHS my ($x) = @array is the same as

    my $x = $array[0];
    

    If parens are used for my, but no values are assigned, then this is only useful to declare multiple variables, so my ($foo); and my $foo; are exactly equivalent.

  3. The $DBHandle->selectrow_hashref aims to return a reference to a hash. When this is assigned to the $ptBatchRec variable, yes, that variable will hold a reference.

    But that variable can hold any type of scalar, of which hash references are just one type.

amon
  • 57,091
  • 2
  • 89
  • 149