Solved

[@$]

Posted on 1998-07-17
2
115 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

24 Experts available now in Live!

Get 1:1 Help Now