Solved

[@$]

Posted on 1998-07-17
2
116 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
ID: 1208489
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
ID: 1208490
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

11 Experts available now in Live!

Get 1:1 Help Now