Loading ...
Sorry, an error occurred while loading the content.

Re: uninitialized value (was my what?)

Expand Messages
  • Charles K. Clarkson
    ... Chances are $group is the culprit, not @mailboxes. Reread the split description in perlfunc. As a special case, specifying a PATTERN of space ( ) will
    Message 1 of 2 , May 7, 2001
    • 0 Attachment
      Greg Matheson <lang@...>:


      :
      : I ask because I thought the reason I was gettign the following
      : error message:
      :
      : > Use of uninitialized value at Anon.pm line 68, <PAIRS> chunk 27.
      :
      : for the following subroutine,
      :
      : > sub studentboxes {
      : > my $self = shift;
      : > my @mailboxes = ();
      : > open (PAIRS, "groups.alt") or die "CAn't open pairs:$!\n";
      : > LINE: while (my $line = <PAIRS>) {
      : > next LINE if $line =~ /^$/;
      : > chop $line;
      : > my ($group, $first, $second, $third) = split (/ /, $line, 5);
      : > push @mailboxes, ( "=" . $group . "/" . $first ,
      : > "=". $group . "/" . $second , "=" . $group . "/" . $third );
      : > }
      : > @mailboxes = sort byid @mailboxes;
      : > return @mailboxes;
      : > }
      :
      : [Line 68 is: push @mailboxes, ( "=". $group . "/" etc etc ]
      : was because I define my @mailboxes outside the while loop. What
      : else could be undefined?
      :
      : And that's the problem. I don't want to "litter in the park", but
      : I also don't want the error message. So how can I push onto my
      : array within the while loop and then still have it defined at the
      : end of the loop while still making it a lexically scoped
      : variable?
      :

      Chances are $group is the culprit, not @mailboxes.
      Reread the split description in perlfunc.

      As a special case, specifying a PATTERN of space (' ')
      will split on white space just as split with no arguments
      does. Thus, split(' ') can be used to emulate awk's default
      behavior, whereas split(/ /) will give you as many null
      initial fields as there are leading spaces.


      If any of your lines have leading spaces split (/ /, $line, 5)
      will return some undef values. You can test this with
      Data::Dumper:

      use Data::Dumper; # put this at the beginning of script

      Add this line before line 68:

      print Dumper split (/ /, $line, 5);

      If one of the values comes back as '', that's your undef.

      HTH,
      Charles K. Clarkson

      Some people, when confronted with a problem,
      think 'I know, I'll use regular expressions.'
      Now they have two problems.
      - Jamie Zawinski, on comp.lang.emacs
    • Greg Matheson
      I asked a question about an error message I thought was because a lexically scoped variable with my was undefined within a following block. ... Aaarrrghhhh!!
      Message 2 of 2 , May 8, 2001
      • 0 Attachment
        I asked a question about an error message I thought was because
        a lexically scoped variable with 'my' was undefined within a
        following block.

        On Mon, 07 May 2001, Charles K. Clarkson wrote:

        > :
        > : > sub studentboxes {
        > : > my $self = shift;
        > : > my @mailboxes = ();
        > : > open (PAIRS, "groups.alt") or die "CAn't open pairs:$!\n";
        > : > LINE: while (my $line = <PAIRS>) {
        > : > next LINE if $line =~ /^$/;
        > : > chop $line;
        > : > my ($group, $first, $second, $third) = split (/ /, $line, 5);
        > : > push @mailboxes, ( "=" . $group . "/" . $first ,
        > : > "=". $group . "/" . $second , "=" . $group . "/" . $third );
        > : > }
        > : > @mailboxes = sort byid @mailboxes;
        > : > return @mailboxes;
        > : > }
        > :

        > Chances are $group is the culprit, not @mailboxes.

        Aaarrrghhhh!! yes, I assumed my file, group.alt was
        OK, but the last record in it only had three fields instead of
        four.

        On Mon, 07 May 2001, Andrew Johnson wrote:

        > Yes, it does. A lexical can be accessed anywhere in the current
        > block, including inside of nested blocks within the current
        > block (unless a new lexical of the same name is declared within
        > the nested block, masking the outer one).

        I guess I was assuming that if a lexical declared with my wasn't
        defined in a subroutine even if it was the same subroutine calling
        itself, then the same applied to a nested block too.

        JE Cripps also helped me out with the split statement not doing
        something you expect when the the data doesn't have something
        there you assumed would be there.

        --
        Greg Matheson All teaching is teaching
        Chinmin College, under difficult circumstances.
        Taiwan

        --
        Greg Matheson Practitioners just do it.
        Chinmin College, Reflective Practitioners just think they
        Taiwan did it.
      Your message has been successfully submitted and would be delivered to recipients shortly.