[29869] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1112 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Dec 13 11:14:17 2007

Date: Thu, 13 Dec 2007 08:14:10 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 13 Dec 2007     Volume: 11 Number: 1112

Today's topics:
    Re: split is not convinient <tadmc@seesig.invalid>
    Re: split is not convinient <joost@zeekat.nl>
    Re: split is not convinient <1usa@llenroc.ude.invalid>
    Re: split is not convinient <ben@morrow.me.uk>
    Re: split is not convinient <cwilbur@chromatico.net>
    Re: Stupid Mistakes <inthepickle@gmail.com>
    Re: Stupid Mistakes <ben@morrow.me.uk>
    Re: Stupid Mistakes <josef.moellers@fujitsu-siemens.com>
    Re: Stupid Mistakes <ben@morrow.me.uk>
    Re: Stupid Mistakes <ben@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Thu, 13 Dec 2007 06:09:38 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: split is not convinient
Message-Id: <slrnfm2882.s8j.tadmc@tadmc30.sbcglobal.net>

Jürgen Exner <jurgenex@hotmail.com> wrote:
> Mark Seger wrote:

>> In fact I avoid any syntax that
>> has a default such as "while (<>)" which is why I always use "while
>> ($var=<>)". 


I agree completely with this one.

Actually, it should most likely be:

    while ( my $var = <> )


>> Are people that lazy that typing a few extra characters to
>> make what they're doing more explicit too much of a burden?


Many are. I am not one of them though.  :-)


> I am somewhat torn. You certainly have a very valid point and taking 
> shortcuts that lead to unreadable code is definitely not a good idea.


Understanding the code is not the big reason for avoiding while (<>).

Stomping on $_ without even local()izing it is the big reason IMO, it
can lead to classic "action at a distance".


>> How is
>> this any different from commenting code?  Or don't people do that any
>> more either?
>
> Now that is actually comparing apples and oranges. I think you would agree 
> that a comment like
>     $index++ #increments $index by 1
> is nonsense. 


It makes perfect sense.


> Correct, but still nonsense. 


   Correct, but without value

I could buy that one though. Or even


   Correct, but with _negative_ value

(because now the code must be kept in sync with the comment)


> Comments should describe _what_ is 
> being done, 


I think you are misremembering the classic advice regarding comments.

The code already describes what is being done. Saying the same thing
twice does not add any value.

   Comments should describe _why_ it is being done.


> not _how_ it is done. So the comment above should be something 
> like "examine next personal file" or whatever action this increment 
> indicates.


That comment is a "why" rather than a "what".

Q: Why is the programmer incrementing $index

A: To move on to processing the next personal file


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: 13 Dec 2007 14:24:27 GMT
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: split is not convinient
Message-Id: <4761409b$0$16792$e4fe514c@dreader27.news.xs4all.nl>

On Thu, 13 Dec 2007 06:09:38 -0600, Tad J McClellan wrote:

> Jürgen Exner <jurgenex@hotmail.com> wrote:
>> Mark Seger wrote:
> 
>>> In fact I avoid any syntax that
>>> has a default such as "while (<>)" which is why I always use "while
>>> ($var=<>)".
> 
> 
> I agree completely with this one.
> 
> Actually, it should most likely be:
> 
>     while ( my $var = <> )
> 

As has been mentioned somewhere above, that's simply incorrect code in 
most circumstances. while (<handle>) { ... } is a special case and if you 
really must use a named variable the construct should be

while (defined(my $var = <>)) { ... }

Joost.




------------------------------

Date: Thu, 13 Dec 2007 15:13:29 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: split is not convinient
Message-Id: <Xns9A056803382C5asu1cornelledu@127.0.0.1>

Joost Diepenmaat <joost@zeekat.nl> wrote in
news:4761409b$0$16792$e4fe514c@dreader27.news.xs4all.nl: 

> On Thu, 13 Dec 2007 06:09:38 -0600, Tad J McClellan wrote:
> 
>> Jürgen Exner <jurgenex@hotmail.com> wrote:
>>> Mark Seger wrote:
>> 
>>>> In fact I avoid any syntax that
>>>> has a default such as "while (<>)" which is why I always use "while
>>>> ($var=<>)".
>> 
>> 
>> I agree completely with this one.
>> 
>> Actually, it should most likely be:
>> 
>>     while ( my $var = <> )
>> 
> 
> As has been mentioned somewhere above, that's simply incorrect code in
> most circumstances. while (<handle>) { ... } is a special case and if
> you really must use a named variable the construct should be
> 
> while (defined(my $var = <>)) { ... }

As far as I know, the magic test for defined in a while loop is the same 
whether you are reading into a $_ or any lexical variable. Here is a 
short test:

C:\DOCUME~1\asu1\LOCALS~1\Temp\m> cat m.txt
0
1

2
3

4
C:\DOCUME~1\asu1\LOCALS~1\Temp\m> xxd m.txt
0000000: 300a 310a 0a32 0a33 0a0a 340a 0a         0.1..2.3..4..

C:\DOCUME~1\asu1\LOCALS~1\Temp\m> cat m.pl
#!/usr/bin/perl

use strict;
use warnings;

my $name = 'm.txt';

my %dispatch = (
    without_defined => \&without_defined,
    with_defined    => \&with_defined,
    without_var     => \&without_var,
);

for my $case ( sort keys %dispatch ) {
    print "$case:\n";

    open my $f, '<', $name
        or die "Cannot open '$name': $!";
    $dispatch{ $case }->( $f );
    close $f
        or die "Cannot close '$name': $!";
}

sub without_defined {
    my $f = shift;

    while ( my $line = <$f> ) {
        print decorate($line);
    }
}

sub with_defined {
    my $f = shift;

    while ( defined( my $line = <$f> ) ) {
        print decorate($line);
    }
}

sub without_var {
    my $f = shift;

    while ( <$f> ) {
        print decorate($_);
    }
}

sub decorate {
    my ($s) = @_;
    $s =~ s/\n/</;
    return "'$s'\n";
}

__END__

C:\DOCUME~1\asu1\LOCALS~1\Temp\m> m
Without var assignment in while:
'0<'
'1<'
'<'
'2<'
'3<'
'<'
'4<'
'<'
With 'defined' in while:
'0<'
'1<'
'<'
'2<'
'3<'
'<'
'4<'
'<'
Without 'defined' in while:
'0<'
'1<'
'<'
'2<'
'3<'
'<'
'4<'
'<'





-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>



------------------------------

Date: Thu, 13 Dec 2007 15:18:51 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: split is not convinient
Message-Id: <r3v735-jpj2.ln1@osiris.mauzo.dyndns.org>


Quoth Joost Diepenmaat <joost@zeekat.nl>:
> As has been mentioned somewhere above, that's simply incorrect code in 
> most circumstances. while (<handle>) { ... } is a special case and if you 
> really must use a named variable the construct should be
> 
> while (defined(my $var = <>)) { ... }

while (my $var = <HANDLE>) gets the same special-case treatment:

    ~% perl -MO=Deparse -e'while (my $x = <HANDLE>) { 1 }'
    while (defined(my $x = <HANDLE>)) {
        '???';
    }
    -e syntax OK

Ben



------------------------------

Date: 13 Dec 2007 10:41:33 -0500
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: split is not convinient
Message-Id: <87d4ta61qq.fsf@mithril.chromatico.net>

>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:

    JE> On the other hand a lot of the elegance of Perl comes from its
    JE> default "work space". It is so ingrained in the Perl world
    JE> that I would almost call it a prerequisit for programming in
    JE> Perl. If you don't know how to take advantage of $_ and @_
    JE> then maybe you shouldn't program in Perl or maintain Perl
    JE> code.

I would add, here, that if you don't know when to *not* take advantage
of $_ and @_, you shouldn't program in Perl or maintain Perl code,
either -- and that knowing *when* and *whether* to use a feature of a
programming language is far more important than knowing *how* to use
that feature.

Charlton



-- 
Charlton Wilbur
cwilbur@chromatico.net


------------------------------

Date: Thu, 13 Dec 2007 05:47:21 -0800 (PST)
From: inthepickle <inthepickle@gmail.com>
Subject: Re: Stupid Mistakes
Message-Id: <39d1653b-d83b-4a94-ade0-aac8b60e0712@i12g2000prf.googlegroups.com>

Thanks for all of the input.  I am no programmer and I am fairly new
to Perl.  I got a lot of good suggestions here, and I will try to
incorporate them.

Thanks again.


------------------------------

Date: Thu, 13 Dec 2007 14:40:34 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Stupid Mistakes
Message-Id: <2ss735-k5i2.ln1@osiris.mauzo.dyndns.org>

[please don't top-post]

Quoth Josef Moellers <josef.moellers@fujitsu-siemens.com>:
> Why so complicated? You don't need another call to findswfile(): just 
> set $File::Find:prune = 1 at the place where you decide that you don't 
> need to go further.

The OP was attempting the search in this order:

    Y:/BASEDIR/SUBDIR
    Y:/BASEDIR
    Y:/

, the point being that Y:/BASEDIR/SUBDIR is searched *before* the rest
of Y:/BASEDIR (presumably s?he feels the file is more likely to be found
there). While this could be emulated with one find, with bydepth => 1
and preprocess set to a sub that sorted BASEDIR/SUBDIR and BASEDIR first
in their respective directories, three finds are probably just as clear,
especially given the OP's apparent inexperience with Perl. Searching
SUBDIR three times I would expect to have negligible effect, as the
directories will all be in the cache.

Ben



------------------------------

Date: Thu, 13 Dec 2007 16:03:26 +0100
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: Stupid Mistakes
Message-Id: <fjrhk2$9ab$1@nntp.fujitsu-siemens.com>

Ben Morrow wrote:
> [please don't top-post]

I didn't top-post, I quoted your code and amended it ;-)

As for your reply: I don't quite understand how setting a global=20
variable and checking it immediately on the next entry to findswfile()=20
and aborting *then* is better than aborting immeiately the file is found?=


--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize (T.  Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html



------------------------------

Date: Thu, 13 Dec 2007 15:32:11 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Stupid Mistakes
Message-Id: <rsv735-jpj2.ln1@osiris.mauzo.dyndns.org>


Quoth Josef Moellers <josef.moellers@fujitsu-siemens.com>:
> Ben Morrow wrote:
> > [please don't top-post]
> 
> I didn't top-post, I quoted your code and amended it ;-)

Not my code. You were replying to 'A. Bax'.

> As for your reply: I don't quite understand how setting a global 
> variable and checking it immediately on the next entry to findswfile() 
> and aborting *then* is better than aborting immeiately the file is found?

Ah. I misunderstood you. Had your reply been properly below the piece of
code you were talking about, that wouldn't have happened. ;)

Ben



------------------------------

Date: Thu, 13 Dec 2007 15:37:48 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Stupid Mistakes
Message-Id: <c70835-jpj2.ln1@osiris.mauzo.dyndns.org>

[please wrap articles at ~72 characters]

Quoth "A. Bax" <c7eqjyg02@sneakemail.com>:
>         # matching of a literal full stop; collapse /\.sldprt/ || /
> \.SLDPRT/
>         # into /\.sldprt/i .

These don't do the same thing. The latter matches '.SlDpRt'. This may or
may not be important to the OP.

> There is still a big performance hit
> because the
>         # regexp /$swfile/ must be compiled for every call to
> findswfile(), but
>         # fixing that requires more work.

No, there isn't. Perl keeps track of which variables were interpolated
into a regex, and only recompiles when they are changed. Thus, this will
only be recompiled once for each time through the outermost loop, as
desired.

>             our $f = $File::Find::name;        #directory and file
> name

Don't use our for simple file-scope variables. Use my at the top of the
file. our is for when you really need a package variable: generally,
when code outside this file needs access to it.

Ben



------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 1112
***************************************


home help back first fref pref prev next nref lref last post