Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How to goto new sub if file empty?

by Anonymous Monk
on Apr 23, 2003 at 05:19 UTC ( #252453=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey everyone :)

I am very new to Perl and can't understand why I can't check to see if a file is empty in a while loop...

I am building a page for my teacher to be able to change links on a school site anytime he wants through his browers. My script opens a file, reads the lines, splits the contents of the line into an array and displays them (not like you don't understand this already... don't mean to offend any of you :) )

Anyway, if the file is empty, I am trying to redirect to a subroutine called stopit..

open(FILE, "links.dat") || die "Can't open file!"; flock(FILE, 2) || die "Can't lock file!"; while ($line = <FILE>) { chomp $line; my @part = split(/\|/,$line); if ($part[0] eq "") { &stopit; } if ($part[3] eq "new") { $which = " target=\"_blank\""; } print "<li><a href=\"$part[2]\"$which>$part[1]</a></li><br>\n"; } close(FILE); print "</ul></body></html>"; sub stopit { close(FILE); print "There are currently no links :("; exit; }

What am I doing wrong? I thought I could check to see if the first part of the array has a value near the beginning og the while loop?

I know the coding is very crude... please be gentle!!

Replies are listed 'Best First'.
Re: How to goto new sub if file empty?
by pfaut (Priest) on Apr 23, 2003 at 05:25 UTC

    If the file is empty, the first attempt to read from it returns false (undef) and you never execute the body of the while loop. Your call of &stopit is inside that body so it never gets called. You can check for an empty file with eof before executing the while.

    open(FILE, "links.dat") || die "Can't open file!"; flock(FILE, 2) || die "Can't lock file!"; stopit if eof FILE; while ($line = <FILE>) { ....
    90% of every Perl application is already written.
    dragonchild
      Thanks a million pfaut!

      That was exactlly what I was trying to figure out... how to check for content in the file before I assigned it to a variable and started a loop :)

      Thanks again!!

Re: How to goto new sub if file empty?
by robartes (Priest) on Apr 23, 2003 at 05:55 UTC
    Another way of checking the emptiness of a file, without even opening it, is using the -z filetest operator:
    stopit() if -z $file;

    CU
    Robartes-

Re: How to goto new sub if file empty?
by vek (Prior) on Apr 23, 2003 at 06:07 UTC
    Something else you could try is a file test before attempting to open the file:
    if (-s $file) { open(FILE, $file) || die "open: $file - $!\n"; # etc } else { warn "There are currently no links :(\n"; # or similar msg exit(); }
    Take a peek at this for a list of file test operators.

    -- vek --
Re: How to goto new sub if file empty?
by Anonymous Monk on Apr 23, 2003 at 07:32 UTC
    Wow, thanks to everyone for all your help!!!

    Got it working perfect!

    robj :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://252453]
Approved by pfaut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2021-04-21 09:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?