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

Re: [MacPerl] & Usage and Misc. ?'s



At 09:36 -0800 11/2/98, Mark Yannuzzi wrote:
>
> 3) I noticed the use of the "&" character in a non-logical operator
> context, in an example on pg. 186.

    $file = StandardGetFile(\&myFilter, 0);

> I gather that it has something to do with calling subroutines, but
> the construct is not introduced anywhere in the text, it would be helpful

&function is the (old) way to call a named subroutine.  It's no longer
necessary unless the call would otherwise be ambiguous so we didn't mention
it.  (That is, foo() is unambiguously a function call, &foo is also
unambiguous; foo is not unambiguous)

However, there should have been a footnote in chapter 8 for historical
purposes.

In this particular example on p. 186, however, something else is going on.
\& is a _reference_ to a subroutine.  References were covered in ch. 8
(including the reference operator, the \, and an example of a reference to
a scalar variable, \$var) but since we didn't cover & for function calls,
\& isn't obvious to figure out.  Sorry 'bout that.  We goofed.

> if a sentence or two were included about  this usage, and how it differs
> from simply calling a subroutine without it.

Basically:

     &subname(...)
and
     subname(...)

are equivalent. But

    \&subname
and
    &subname

are quite different (the former is a reference, the latter calls the
function)

>I have discovered through trial and error that the following two
> statements are equivalent, on the surface:
>
>   &StandardFile'GetFolder() & StandardFile::GetFolder()
                              ^
                     I->-->->-I
                     ^
I presume the second & is simply your shorthand for the word "and" ?

then yes, the former is the old (Perl 4) way, using the old, less used &
before the function name AND ALSO using the deprecated single tick, ',
after the library name.

The latter is the Perl 5 way, leaving out the & in the function call
(because the () make it a non-ambiguous statement) and using the new,
not-deprecated, :: syntax.

You could say &StandardFile::GetFolder() but there is no real need to do so.

> 4) pg. 187 has an example containing a line that appears to be a shortcut:
>
> $answer = (MacPerl::Answer('Erase existing file?', 'No', 'OK')) if
> ($file->sfReplacing());

The reversed use of if (and unless) should also have been covered in
chapter 8.  We'll do better next time. (In our defense, I just paged
through the Camel;  this kind of if is used several times, early in the
book. e.g. p. 25
     last LINE if $line eq "\n"; ...
but I can't find anyplace where it is _explained_, not even when they
explain if in chapter two.

>
> that suggests that the "if" statement is executed first, and if TRUE, the
> statement preceding it is executed (bizarre),

Well, no, not so bizarre.  Unusua,l perhaps, in a programming language but
consider the following:

    Get your coat if you're ready to go
    I'll take ham and eggs if you have any eggs
    Check if employed
    Print if finished
    Go unless the light is red

English is full of such statements.  Why not Perl?

>Perhaps you could include a brief discussion of this along
> with your other shortcut examples (the ?: construct, etc.).

Next time.  And the errata for now.

- Vicki
---
Vicki Brown <vlb@cfcl.com>           |\      _,,,---,,_
Journeyman Sourceror           ZZZzz /,`.-'`'    -.  ;-;;,_
Scripts & Philtres                  |,4-  ) )-,_. ,\ (  `'-'
P.O. Box 1269, San Bruno CA 94066  '---''(_/--'  `-'\_)
http://www.cfcl.com/~vlb      http://www.ptf.com/macperl