PDA

Click to See Complete Forum and Search --> : Perl: references (bug?)


CaptainPinko
Jul 17th, 2002, 08:46 PM
I'm not sure if this is a bug or my misunderstanding of the Perl syntax. I'm having problems with using references.:(


sub printArray {
my @array = @($_[0]);

my $prefix = (defined $_[1])? $_[1]: "";

foreach my $item (sort @array)
{print $prefix.$item."\n";}
}


my $root = "e:\\";

sub printArray;

opendir (DIR, $root);
my @contents = readdir (DIR);

printArray (\@contents);


I keep on getting an error on with, apparently, @array being unitialized. Upon further investigation it appears that the problem is in the assignment to @array.:eek: Apparently it doesn't like scalar symbol not directly being followed by another variable type character (ie. $ @ % & etc.) or a variable name.
I tried removing the brackets around $_[0], but then it seemd to be trying to perform the deferencing on just the $_; not including the [0]; I found a workaround by: :mad:

my $temp = $_[0];
my @array = @$temp;


but this is an ugly workaround, wastes memory (ie. extra variable), and its ugly/cumbesome/annoying/etc.. :rolleyes:It also seems very unPerl-ish. Is this a bug or is this intentional? If its intentional anyone care to explain WHY?!? Does anyone else have this problem or this just w/ my interpreter?:confused:

phpman
Jul 26th, 2002, 08:12 AM
not a bug in PERL, a bug in the script. If you just want to print the contents of a dir why not just print it instead of trying to hand it off to a sub?

my $root = "e:\\";
opendir(DIR, "$root");
@files = sort(readdir(DIR));
closedir(DIR);
foreach (@files) {
print "$_<br>\n";
}

if you really want to use the sub, maybe something like this:

sub printArray {
my @array = @_;

my $prefix = (defined $_[1])? $_[1]: "";

foreach my $item (@array) {
print $prefix.$item."\n";
}
}

CaptainPinko
Sep 29th, 2002, 05:02 PM
Originally posted by phpman
not a bug in PERL, a bug in the script. If you just want to print the contents of a dir why not just print it instead of trying to hand it off to a sub?


how is that an error in the script? its just seems like a difference in approach to me... the sub that i was passing it to formated the output and did our tasks that were core to the prog.

phpman
Oct 3rd, 2002, 06:03 PM
jeez 2 months later. ;)

difference in approach but mine doesn't seem to be a bug.

if they are so different then why complain about a bug in Perl.

JoshT
Oct 7th, 2002, 09:22 PM
Off the top of my head, I'm going to guess something like this would work.

sub printArray {
my $array = shift;

print $array->[0];

}

printArray (\@contents);

CaptainPinko
Oct 7th, 2002, 11:45 PM
i don't remember seeing a -. operator, what is it?

JoshT
Oct 8th, 2002, 10:05 AM
You mean -> - its a dereference operator, used heavlily in OO perl for calling methods of objects. But basically, you passed a scalar reference o the array, so you dereference the scalar to get at the array it references.


use CGI;
my $cgi = new CGI;
print $cgi->param('formfield');