Mr. Shawn H. Corey wrote:
On Mon, 2008-10-27 at 17:05 +0000, Brian wrote:

Why is it that some code has no curly braces after print; whilst in others I sometimes see one or more curly braces after it? Will there be a time when print; will fail because there isn't a curly brace following it, even though an equal number of left & right braces precede it? I appreciate I have the right to lay out my code in any way I see fit, I would just like to see the reasoning.

An example of something confusing me is in the sample below
find sub {
       return unless -f;
       open my $FH, '<', $_ or die "Cannot open '$_' $!";
       while ( <$FH> ) {
           /\Q$string/ && print $REPORT "$File::Find::name\n" and
  return;
           }}, '/test';

Why isn't the last line
          }, '/test';}

This code is written this way because the coder has been playing too
much Perl Golf ;)

Not hardly likely.  :-(

The objective of Perl Golf is to write a program to
do a simple task in the least number of characters possible.  This, of
course, makes it harder to read.

In your opinion.  ;-)

Another way to write the above:

# wanted -- a sub for File::Find::find()
#
#   $_ contains the basename of the file
#
#   $File::Find::name contains the full path to the file
#
#   $string is a global containing the string to look for;
#           it is NOT a regular expression;
#           all characters in it are matched
#
#   $REPORT is a global file handle to the output file;
#           it must be opened for writing
#

#  $string and $REPORT are "global" because 'wanted' is a
#  callback function for File::Find::find and as such you
#  cannot pass arguments to it like a "normal" subroutine.

sub wanted {
    return unless -f $File::Find::name;  # ignore directories, symbolic links, 
etc.

In what way does -f ignore symbolic links? It does follow symbloic links if that is what you mean. If you wanted it to "ignore" symbolic links you would have to lstat() first:

      lstat;
      return unless -f _;

Or perhaps:

      return if -l || !-f _;

    open my $FH, '<', $_ or die "Cannot open '$_' $!";
    while (<$FH>) {
        if (/\Q$string/) {
            print $REPORT "$File::Find::name\n";
            return;
        }
    }

    return;

Since this is a callback function the value you are returning is ignored.

}

find( \&wanted, '/test' );


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to