Solved

[@$]

Posted on 1998-07-17
2
114 Views
Last Modified: 2010-03-05
I'm reading a manual on sybperl, a sybase module for perl. One function returns a reference to a static array (a row from the database) and this reference is stored in $d.
The manual says that to copy the array into a list of arrays, I need to do something like:

push(@rows, [@$d]) ;

My understanding is that the @ dereferences $d, while the [] create a reference to the list.
How and why does the procedure above create a copy of the array that $d references instead of just referencing the same thing? In other words, why is [@$d] not the same thing as $d?
Thanks,

Robert
0
Comment
Question by:lendvai
2 Comments
 
LVL 6

Expert Comment

by:alamo
Comment Utility
As I understand it, the [] produces a reference to an anonymous array, not the original array. Compare that to \@$d which I think would be equivalent to $d.
0
 
LVL 5

Accepted Solution

by:
b2pi earned 100 total points
Comment Utility
Basically, [@$d] makes a copy of the array referenced by $d, and returns a reference to it.

A good way of playing with these things is to use the perl debugger:


(frev40)/u/[4]%perl -dw
 
Loading DB routines from perl5db.pl version 1.01
Emacs support available.
 
Enter h or `h h' for help.
 
use strict;
 
my(@a, $d, @rows);
@a = (1,2,3);
$d = \@a;
push(@rows, $d); # Puts the scalar $d (a reference to @a) on @rows
push(@rows, @$d); # puts the values in $d onto @rows);
push(@rows, [@$d]); #puts a reference to a copy of $d on @rows
 
$d->[0] = 10;
 
print "All Done\n";
^D [^Z for Win32]
 
main::(-:3):    my(@a, $d, @rows);
  DB<1> n
main::(-:4):    @a = (1,2,3);
  DB<1> n
main::(-:5):    $d = \@a;
  DB<1> n
main::(-:6):    push(@rows, $d); # Puts the scalar $d (a reference to @a) on @rows
  DB<1> n
main::(-:7):    push(@rows, @$d); # puts the values in $d onto @rows);
  DB<1> n
main::(-:8):    push(@rows, [@$d]); #puts a reference to a copy of $d on @rows
  DB<1> n
main::(-:10):   $d->[0] = 10;
  DB<1> n
main::(-:12):   print "All Done\n";
  DB<1> x @rows
0  ARRAY(0x18c924)
   0  10
   1  2
   2  3
1  1
2  2
3  3
4  ARRAY(0xb3948)
   0  1
   1  2
   2  3
  DB<2> q


Shows the difference pretty well... By the way, I generally recommend that people not use sybperl, and go directly to using DBlib ...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now