error but code works

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default error but code works

    Hi folks,

    I've got the following code which works, but generates the error message
    following it. I've tried varying the code to eliminate the error but I only
    manage to get either the working code with error or non-working code without
    error.

    (Line 95 is the while line)

    print "checking file $fname...";
    open DAT,"<$fname" ||die "cannot open $fname: $!\n";
    my $found=0;
    while ((my $line=<DAT>) && (! $found)) {
    print "line=$line" if ($DEBUG > 0);
    $found=1 if $line=~/$localpart:/;
    }
    close DAT;

    root@stan input]# tidy-mailq|gvim -
    Value of <HANDLE> construct can be "0"; test with defined() at
    /root/bin/tidy-mailq line 95.
    Vim: Reading from stdin...
    [root@stan input]#
    --
    Gary Stainburn

    This email does not contain private or confidential material as it
    may be snooped on by interested government parties for unknown
    and undisclosed purposes - Regulation of Investigatory Powers Act, 2000

    Gary Stainburn Guest

  2. Similar Questions and Discussions

    1. ASP code works everywhere except Mac OS9 IE
      I have an ASP page that loads fine on all browsers and platforms that I've checked except for IE5 on a MacOS9....
    2. code no longer works from ColdFusion 5
      Any Idea why this code works in ColdFusion 5 and not in ColdFusion MX <CFQUERY NAME="WaasSUMQuery" DATASOURCE="WAASAccount"> SELECT *...
    3. Code no longer works
      I have a machine that I was using as a web server that I recently changed the operating system from Windows 2000 Professional to Windows Advanced...
    4. Check this code. Works in 5, not in 6!!
      Hi. I'm trying to get the .focus to change from textfield to the next by pressing the TAB key. This code works in a Flash 5 movie, but not a 6. The...
    5. Code works well in A97 and Xp, but bombs in A2000.
      Here is a procedure which produces a duplicate record in an order entry program, along with subform data. It has worked well for years in A97 and I...
  3. #2

    Default RE: error but code works

    Gary Stainburn wrote:
    > Hi folks,
    >
    > I've got the following code which works, but generates the error
    > message following it. I've tried varying the code to eliminate the
    > error but I only manage to get either the working code with error or
    > non-working code without error.
    >
    > (Line 95 is the while line)
    >
    > print "checking file $fname...";
    > open DAT,"<$fname" ||die "cannot open $fname: $!\n";
    > my $found=0;
    > while ((my $line=<DAT>) && (! $found)) {
    > print "line=$line" if ($DEBUG > 0);
    > $found=1 if $line=~/$localpart:/;
    > }
    > close DAT;
    >
    > root@stan input]# tidy-mailq|gvim -
    > Value of <HANDLE> construct can be "0"; test with defined() at
    > /root/bin/tidy-mailq line 95.
    > Vim: Reading from stdin...
    > [root@stan input]#
    From perldoc perldiag:

    Value of %s can be ""0""; test with defined()
    (W misc) In a conditional expression, you used <HANDLE>, <*>
    (glob), "each()", or "readdir()" as a boolean value. Each of these
    constructs can return a value of "0"; that would make the condi-
    tional expression false, which is probably not what you intended.
    When using these constructs in conditional expressions, test their
    values with the "defined" operator.

    changing:
    while ((my $line=<DAT>) && (! $found)) {

    to
    while (defined(my $line=<DAT>) && (! $found)) {

    should make the warning go away.
    Bob Showalter Guest

  4. #3

    Default Re: error but code works

    On Jan 29, 2004, at 8:07 AM, Gary Stainburn wrote:
    > Hi folks,
    >
    > I've got the following code which works, but generates the error
    > message
    > following it. I've tried varying the code to eliminate the error but
    > I only
    > manage to get either the working code with error or non-working code
    > without
    > error.
    >
    > (Line 95 is the while line)
    >
    > print "checking file $fname...";
    > open DAT,"<$fname" ||die "cannot open $fname: $!\n";
    > my $found=0;
    > while ((my $line=<DAT>) && (! $found)) {
    > print "line=$line" if ($DEBUG > 0);
    > $found=1 if $line=~/$localpart:/;
    > }
    >
    How about we simplify that loop a little:

    while (<DAT>) {
    print "line=$_" if $DEBUG;
    last if /$localpart/;
    }

    That's a little easier on the eyes, I think. It means the same and it
    will remove the error.

    Hope that helps.

    James
    > close DAT;
    >
    > root@stan input]# tidy-mailq|gvim -
    > Value of <HANDLE> construct can be "0"; test with defined() at
    > /root/bin/tidy-mailq line 95.
    > Vim: Reading from stdin...
    > [root@stan input]#
    > --
    > Gary Stainburn
    >
    > This email does not contain private or confidential material as it
    > may be snooped on by interested government parties for unknown
    > and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
    >
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >
    >
    James Edward Gray II Guest

  5. #4

    Default Re: error but code works

    On Jan 29, Gary Stainburn said:
    >I've got the following code which works, but generates the error message
    >following it. I've tried varying the code to eliminate the error but I only
    >manage to get either the working code with error or non-working code without
    >error.
    It's not an error message, it's a warning. And it tells you what to do.
    > open DAT,"<$fname" ||die "cannot open $fname: $!\n";
    > my $found=0;
    > while ((my $line=<DAT>) && (! $found)) {
    > print "line=$line" if ($DEBUG > 0);
    > $found=1 if $line=~/$localpart:/;
    > }
    > close DAT;
    >Value of <HANDLE> construct can be "0"; test with defined() at
    If you turn on 'use diagnostics', it will explain more about this warning.
    Suffice to say, reading from a filehandle can return JUST the string '0',
    which is technically false, and this will stop your while loop. If you
    don't want that to happen, you wrap the assignment in defined():

    while (defined(my $line = <DAT>) and not $found) { ... }

    But I'd suggest rewriting your code thus:

    while (<DAT>) {
    print "line=$_" if $DEBUG > 0;
    $found = 1, last if /$localpart:/;
    }

    Here, we exit the loop immediately when $found is set. This lets us use a
    simply while clause (<DAT>) which assigns to $_ and tests for defined-ness
    implicitly.

    --
    Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  6. #5

    Default Re: error but code works

    On Thursday 29 Jan 2004 2:25 pm, James Edward Gray II wrote:
    > On Jan 29, 2004, at 8:07 AM, Gary Stainburn wrote:
    > > Hi folks,
    > >
    > > I've got the following code which works, but generates the error
    > > message
    > > following it. I've tried varying the code to eliminate the error but
    > > I only
    > > manage to get either the working code with error or non-working code
    > > without
    > > error.
    > >
    > > (Line 95 is the while line)
    > >
    > > print "checking file $fname...";
    > > open DAT,"<$fname" ||die "cannot open $fname: $!\n";
    > > my $found=0;
    > > while ((my $line=<DAT>) && (! $found)) {
    > > print "line=$line" if ($DEBUG > 0);
    > > $found=1 if $line=~/$localpart:/;
    > > }
    >
    > How about we simplify that loop a little:
    >
    > while (<DAT>) {
    > print "line=$_" if $DEBUG;
    > last if /$localpart/;
    > }
    Hi James,

    my problem was having the result in $found after the loop ended, I've amended
    your code so that I have

    if (/$localpart:/) { $found=1; last;}

    Thanks for the help.

    Gary
    >
    > That's a little easier on the eyes, I think. It means the same and it
    > will remove the error.
    >
    > Hope that helps.
    >
    > James
    >
    > > close DAT;
    > >
    > > root@stan input]# tidy-mailq|gvim -
    > > Value of <HANDLE> construct can be "0"; test with defined() at
    > > /root/bin/tidy-mailq line 95.
    > > Vim: Reading from stdin...
    > > [root@stan input]#
    > > --
    > > Gary Stainburn
    > >
    > > This email does not contain private or confidential material as it
    > > may be snooped on by interested government parties for unknown
    > > and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
    > >
    > >
    > > --
    > > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    > > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    --
    Gary Stainburn

    This email does not contain private or confidential material as it
    may be snooped on by interested government parties for unknown
    and undisclosed purposes - Regulation of Investigatory Powers Act, 2000

    Gary Stainburn Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139