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

Does anyone know what this snippet is testing? The code produces no error messages.
#!/usr/local/bin/perl use strict; if (-s _) { print "Dohhh!\n"; }
Thanks!

Replies are listed 'Best First'.
Re: Conditional Wierdness
by dvergin (Monsignor) on Jul 29, 2001 at 05:16 UTC
    It means:
    Print "Dohhh!" if the file size is non-zero for the file that most recently had a file test or 'stat' done on it. That is, if the previously tested file has anything at all in it.

    See perlfunc:_X for the '-s' file test and, at the very end, the use of the 'solitary underscore' for referring to the previously tested file.

Re: Conditional Wierdness
by !me (Acolyte) on Jul 29, 2001 at 05:33 UTC
    Your testing to see if the size of the file specified by the filehandle of the last stat or file test operator is greater than zero. ( I think )
Re: Conditional Wierdness
by bikeNomad (Priest) on Jul 29, 2001 at 19:42 UTC
    In your case, what would the previously tested file actually be? Since there was no previously tested file, none of the file tests should return true...
      bikeNomad -- Good question.

      I should probably apologize for the original question to begin with. It arose from a problem I recently encountered with a program that I borrowed and extensively rewrote. It has worked fine with few problems since 6/98 but recently stopped displaying daily and weekly links from the six-month calendar view. I did recently upgrade to Perl 5.6.1 (before anything broke) but am reluctant to jump to conclusions.

      I thought that I had brought home my Programming Perl book for the weekend but accidently grabbed Essential System Administration instead. Had I grabbed the right book I wouldn't have needed to ask the question at all.

      I narrowed down the problem to the (-s _) clause in question and felt that posting the larger snippet might raise a lot of questions rather than answer a simple one:

      foreach $day (@dow) { $TestDate = "$year$cur_month$link_num[$day]"; if ($Event_Hash{$TestDate} && (-s _)) { ### URL to display events for a single day. $cal_html = "$EVENT_SUM_URL?Qtype=day&"; $cal_html .= "BegDate=$year$cur_month$link_num[$day]\">"; s|^$day |$cal_html${day}${end_anchor} |; s| $day$| $cal_html${day}${end_anchor}|; s| $day | $cal_html${day}${end_anchor} |; $cal_html = "$CAL_LIST_URL_AUTH?date=$date\">"; } } if (/</) { ### URL to display events for the current week $Wk_HTML = "$EVENT_SUM_URL?Qtype=week&" . "BegDate=$year$cur_month$link_num[$First_DOW]&" . "EndDate=$year$cur_month$link_num[$Last_DOW]&" . "WeekNo=$CurrentWk\">"; $output_pub[$current_line] .= "$_ $Wk_HTML" . "w$end_anchor "; } else { $output_pub[$current_line] .= "$_ "; }
      I doubt that I wrote the (-s _) clause since I normally try to avoid cryptic code whenever possible. In any case it is unnecessary to perform a -s at this point since all the data read from files is already stored in hashes. The biggest mysteries are how I overlooked it to begin with and why did it work for three years without any noticed problems.

      The line in question has been reduced to:

      if ($Event_Hash{$TestDate}) {
      instead of:
      if ($Event_Hash{$TestDate} && (-s _)) {
      Thanks to all for responding.