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

Re: [MacPerl-AnyPerl] Re: macperl-anyperl-digest V1 #24



On Wed, Jun 30, 1999 at 10:41:22AM -0700, Jeremy Andrews wrote:
> Thanks, Ronald, for the numerous tips, especially useful for a two
> week old Perl newbie like myself.  I must say I was a little sad to
> see just how many mistakes I had in that short piece of code...
> 
> I've got a couple more questions...
> 
> > > 	unless (@directories eq "") {
> >
> >@directories will _never_ eq "".  In a scalar context, an array evaluates
> >to the number of elements in the array.  This will be 0 if the array is
> >empty, or a positive integer if the array is not empty, but certainly never
> >the null string.
> 
> My tutorial is the O'Reilly book: Programming Perl (which I must say
> I'm very happy with), and I thought I read early on that the way Perl
> parses variables makes "0" = a boolean FALSE = "" (null).  I've
> tested this by setting a scalar to 0 (such a $foo=0) and then to null
> (such as $foo="") and in either case I'm able to evaluate this with a
> simple:
> if ($foo) { &foo; }
> 
> Does this not work with Arrays?  Perhaps above I could have said:
> unless (!@directories) {	?

It does work with arrays.  In a boolean context, an empty array is false,
and a non-empty array is true.

I would write

if (@directories) {

rather than

unless (!@directories) {

but either one works.


However, in a non-boolean context, "0" is not equal to ""; one is a string
of length 1, and the other is a string of lenght 0.


> In which case, what's the difference between
> if (!@directories)
> and
> if (@directories eq "")
> ?

The difference is that the latter is not a boolean context.  You evaluate
@directories in a scalar context; with zero elements, you get the number 0.
You have a string comparison, so you turn that into the string "0".  Then
you compare that to the string "".  "0" ne "", so the conditional is false,
even when @directories is empty.


> >You should include a useful error message when you die.
> 
> Point well taken...  Actually, I've set up a special error subroutine
> I call when there's an error - I removed this from the code when I
> posted it to this mail group in the interests of brevity.  :)

Fair enough.

[What I _should_ have said was "You should offer a few last words when you
die."  *grin* ]

> >Instead of writing the solution from scratch, I would recommend using the
> >finddepth() function from the File::Find module.
> 
> I figured there HAD to be a simpler way!  :)
> 
> Thanks Jim and Geoff for suggesting rmtree -- I must admit I wasn't
> even aware of the modules yet, not yet that far in my Perl manual.
> Silly me...  Anyway, I'm using File::Path successfully (as well as a
> few other handy modules).
> 

I forgot about File::Path, which must be the simplest way to do this!

Ronald

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org