Sorry for the top posting, but I don't feel like manually
massaging the thread.

Hold on, Dave, before you stop using "&" on subroutines
and prototypes, I'd like to hear Rob's explanation
for both of his claims. This is the first I have ever
heard from one of the regulars here on this mailing list
recommending this.

I use prototypes for most of my code. Like "use strict",
I prefer the slight reinforcement it provides. On the
other hand I rarely use the "&" symbol on a subroutine
unless I'm taking a reference of it like:

  finddepth( \&wanted, $dir );

So Rob, I'd be interested in reading the reasons behind
your recommendations. Is this something that Damian Conway
or Mark Jason Dominus has written on? Thanks.

--
Mike Arms


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Friday, May 07, 2004 12:26 PM
To: Rob Dixon
Cc: [EMAIL PROTECTED]
Subject: Re: undef example

Rob, 

Thanks for the help!  I learned something today from you.  As far as the
ampersand notation and prototypes I was using, I saw that use in the PERL
book I have and did not mention any reason why.  I've been using them for
years without problems.  I will stop using them. 

I also did not use the "__END__" in the script.  For some strange reason I
put that in my message only to distinguish the main routine from the
subroutine.  I do not know why I picked that! 

Do you have any good books that you recommend which are good in describing
these uses and behaviors?  I've been using the O'Reilly camel book called
Programming PERL but it's pretty old (first edition). 

Regards,

Dave



"Rob Dixon" <[EMAIL PROTECTED]> 
05/07/2004 02:28 AM 
        
        To:        <[EMAIL PROTECTED]>,
<[EMAIL PROTECTED]> 
        cc:         
        Subject:        Re: undef example



Hi Dave.

See my answer in-line below.



[EMAIL PROTECTED] wrote:
>
> This may be a lame question and I understand what it's doing, but I would 
> like to know if it is possible to undefine a global EXPR if it's passed 
> into a subroutine?
> 
> Code here reduced to the basics.  After the return from routine 
> closeFtpConn I want, in this case, $MVSFTP undefined.
> 
> .
> my $MVSFTP;
> .
> 
> $MVSFTP = Net::FTP->new( $mvsFtpHost, Debug => $myDebugMode )
>           or &errout("Cannot connect to $mvsFtpHost;");
> .
> .
> &closeFtpConn ( $MVSFTP );

Don't use the ampersand notation unless you specifically need it. If you
don't know what it does differently then you don't need it!

 closeFtpConn($MVSFTP);

is the syntax to use.

> __END__

Perl will stop compilation at this line, so with it placed here
closeFtpConn() will be undefined and you'll get a run-time error.

> sub closeFtpConn ($) {

Like the ampersand notation, don't use prototypes unless you know
what they do, and you need that behaviour. In particular they're
for writing list operators, and you're not using closeFtpConn()
as a list operator. Just

 sub closeFtpConn {
   :
 }

Will do fine.

>    my ( $FTPOBJ ) = @_;

At this point the elements of the @_ array are aliases for the
actual parameters passed in the call. This line copies the value
of the first parameter into the (local) lexical variable $FTPOBJ.

>    $FTPOBJ->quit;
>    .
>    .
>    undef $FTPOBJ;

And this line undefines the lexical variable. It has no effect outside
the subroutine.

> }

However, because the elements of @_ are aliases, you can affect
the actual parameters by manipulating those elements, so

 undef $_[0];

will work for you. Since in this case the parameter is an object
reference it should work fine; but note that if you pass a constant
as the actual parameter then this statement will throw a run-time
error. You should use 'eval' to handle this case if it is a possibility.

HTH,

Rob

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to