[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Does Style Count? (formally, 'file tests')



Here is a case for me to get on some nerves ; )

Chris' analysis of the file below is excellent and very informative, and
at one point he brings a valid stylistic assertion about variable names.
 I would like to go further down that road.

Too often, here on the list and elsewhere, I see Perl code which is hard
to read.  Part of the beauty of Perl, be it MacPerl or otherwise, is
that it can be very powerful, yet easy to understand.  I would like to
see more people discussion on stylistic issues.  We could all use the
help.  I do a lot of altering of code.  Usually the code is written by
someone else and the person is long gone.  Most coders have been there,
done that.  When you code, you want to think to the future, assuming
that you want your code to live on, and you should always be aware that
others may have to wade through your code to make it do new and exciting
things.  That being said....

I hate the tertiary operation!  Any operator that you can't spell, must
be suspect ; )  

Seriously, it is difficult to read at a glance.  It is efficient code,
but it is difficult to read even for experienced programs.  It just does
not flow as nicely as a regular conditional.  Also the _ "thing" is new
to me but is also not so easy to read.  Why not use $_ is it does the
same thing?  You don't really save anything, and too many folks are
going to be unclear about what you are trying to say.

Just my two cents.

Chris Nandor wrote:
> 
> I feel like being charitable today.
> 
> At 14.00 -0500 1998.10.28, PwrSurge wrote:
> >---BEGIN SCRIPT---
> >foreach (@ARGV) {
> >  $_ =~ s/.*:(.*)$/$1/;
> >  $NAME = $1;
> 
> Since you are capturing $1 anyway, why bother doing a substitution?
> 
>    /.*:(.*)$/;
>    $NAME = $1;
> 
> If you do what you did, then you lose the full path to the file and cannot
> get the information about it.
> 
> Also, "$_ =~ s///" is always redundant.  Same with "$_ =~ m//".
> 
> >  $READ = "YES" if -r _;
> 
> _ only does a test on what you last did a test on.  If you have not yet
> done a test, then _ is nothing.  You want $_.  On the other tests, then,
> replace ITEM with _.
> 
> >  $WRITE = "YES" if -w ITEM;
> >  $EXEC = "YES" if -x ITEM;
> >  $KIND = "FLDR" if -d ITEM; elsif $KIND = "FILE";
> 
> This is a syntax error.  Maybe:
> 
>    $KIND = -d _ ? "FLDR" : "FILE";
> 
> Note also that if the value of all of these is undef, '', or 0, then the
> value will carry over from the previous item.  i.e., if one file is
> executable, then the next file will be noted as executable, even if it is
> not:
> 
> MacPerl                              YES   YES  YES FILE 1439048  46.0739004629
> Somefile.txt                         YES   YES  YES FILE    36    8.14099537037
> 
> Since you cannot make them lexical and still have write() see them, use the
> ?: operator again, as above with $KIND:
> 
>   $READ = -r $_ ? "YES" : "NO";
>   $WRITE = -w _ ? "YES" : "NO";
>   $EXEC = -x _ ? "YES" : "NO";
> 
> >  $SIZE = -s ITEM;
> >  $LASTMODIFIED = -M ITEM;
> >  write;
> >}
> 
> >$NAME,$READ,$WRITE,$EXEC,$KIND,$SIZE,$LASTMODIFIED
> 
> I think these should probably be separated by spaces and have lowercase,
> but that is personal style preference.  My new script (with #perl -w at the
> top!):
> 
> #!perl -w
> foreach (@ARGV) {
>   /.*:(.*)$/;
>   $name = $1;
>   $read = -r $_ ? "YES" : "NO";
>   $write = -w _ ? "YES" : "NO";
>   $exec = -x _ ? "YES" : "NO";
>   $kind = -d _ ? "FLDR" : "FILE";
>   $size = -s _;
>   $lastmodified = -M _;
>   write;
> }
> format STDOUT_TOP =
> 
> NAME                                READ WRITE EXEC KIND   SIZE   LAST MODIFIED
> ------------------------------      ---- ----- ---- ---- -------- -------------
> .
> 
> format STDOUT =
> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<      @>>> @>>>> @>>> @>>> @||||||| @>>>>>>>>>>>>
> $name, $read, $write, $exec, $kind, $size, $lastmodified
> .
> 
> --
> Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
> %PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])
> 
> ***** Want to unsubscribe from this list?
> ***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch