Results 1 to 7 of 7

Thread: Perl: references (bug?)

  1. #1

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Perl: references (bug?)

    I'm not sure if this is a bug or my misunderstanding of the Perl syntax. I'm having problems with using references.

    Code:
    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. 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:
    Code:
      my $temp = $_[0];
      my @array = @$temp;
    but this is an ugly workaround, wastes memory (ie. extra variable), and its ugly/cumbesome/annoying/etc.. 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?
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  2. #2
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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";
    }
    }

  3. #3

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332
    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.
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  4. #4
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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.

  5. #5
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Off the top of my head, I'm going to guess something like this would work.

    Code:
    sub printArray {
      my $array = shift;
    
      print $array->[0];
    
    }
    
    printArray (\@contents);
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  6. #6

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332
    i don't remember seeing a -. operator, what is it?
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  7. #7
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    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.

    Code:
    use CGI;
    my $cgi = new CGI;
    print $cgi->param('formfield');
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width