Re: Curly braces and the logic of PERL



On Mon, 2008-10-27 at 17:05 +0000, Brian wrote:
Hello

Years ago I used to work with FORTRAN, RPG GAP 2 and a smidgeon of basic.
Code was written one line at a time, the first section of code was Input
data, the next section was calculations, the last section was output.
(For example, in the sample of code below, the "while" line would have
been written on one line instead of 3)

Why is it "good practice" to write PERL the way it is done?

Much of what is good practise is simply historic; it was done that way
in the past, so it's done that way now. The rule for writing "good"
code is to design it as though a recently-graduated new-hire will be
maintaining it...because they will be. :)


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 ;) 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.

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
#
sub wanted {
return unless -f $File::Find::name; # ignore directories, symbolic links, etc.

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

return;
}

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




--
Just my 0.00000002 million dollars worth,
Shawn

Linux is obsolete.
-- Andrew Tanenbaum

.



Relevant Pages

  • Re: Luthier question: Intonating via thinning braces
    ... Is it possible, in your experience, to alter the intonation of one ... string by thinning the braces, whether one brace or more than one. ... Do you recall if your tweaked guitar had a change in intonation? ...
    (rec.music.classical.guitar)
  • Re: empty list
    ... Dan Smart wrote: ... string, and that strings can be converted to lists, and as previously ... stated I'd be very happy if braces actually constructed lists. ...
    (comp.lang.tcl)
  • Re: Parsing Strings in Enclosed in Curly Braces
    ... x = "{ABC EFG IJK LMN OPQ}" ... Can you have braces legitmately lying around in the string, ... Can you have nested braces, and what are you supposed to do with them: ... Parsing is not an entirely trivial subject, ...
    (comp.lang.python)
  • Re: Dynamic column specification in table update
    ... you need braces to EXEC a string. ... Tibor Karaszi, SQL Server MVP ...
    (microsoft.public.sqlserver.programming)
  • Re: Getting Text Value , in Single line
    ... George Kinley wrote: ... > I have a String, which is long Path of a file ... in Perl golf. ...
    (comp.lang.perl.misc)