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
Re: undef example
Rob Dixon [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: 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. Why? To quote Programming Perl: The official name of a subroutine includes the prefix. A subroutine may be called using the prefix, but the is usually optional. When used with parentheses, the does nothing differently. Including it makes clear that the sub is user-defined. Regards, Martin ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
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
Re: undef example
Thanks Bill. Yes that works! Much thanks. Again I learned something from this list. Rob Dixon also pointed out some mistakes in my coding also. Which is better to use: 1) my ( $FTPOBJ ) = @_; --or-- 2) my $FTPOBJ = shift; I've seen both used a lot. Dave $Bill Luebkert [EMAIL PROTECTED] Sent by: [EMAIL PROTECTED] 05/07/2004 12:06 AM To:[EMAIL PROTECTED] cc:[EMAIL PROTECTED] Subject:Re: undef example [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 ); . . __END__ sub closeFtpConn ($) { my ( $FTPOBJ ) = @_; . $FTPOBJ-quit; . . undef $FTPOBJ; } Try something like : use strict; use Net::FTP; my $mvsFtpHost = 'wherever.com'; my $MVSFTP = Net::FTP-new($mvsFtpHost, Debug = 1) or die Cannot connect to $mvsFtpHost;; closeFtpConn (\$MVSFTP); sub closeFtpConn { my $FTPOBJ = shift; $$FTPOBJ-quit; undef $$FTPOBJ; } __END__ -- ,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED] (_/ / ) // //DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /-- o // // Castle of Medieval Myth Magic http://www.todbe.com/ -/-' /___/__/_/_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: undef example
Martin, Thanks for the input. That is where I learn that and therefore the assumption I was making too. Dave Martin Leese [EMAIL PROTECTED] 05/07/2004 01:18 PM To:[EMAIL PROTECTED] cc:Rob Dixon [EMAIL PROTECTED], [EMAIL PROTECTED] Subject:Re: undef example Rob Dixon [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: 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. Why? To quote Programming Perl: The official name of a subroutine includes the prefix. A subroutine may be called using the prefix, but the is usually optional. When used with parentheses, the does nothing differently. Including it makes clear that the sub is user-defined. Regards, Martin ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: undef example
[EMAIL PROTECTED] wrote: Thanks Bill. Yes that works! Much thanks. Again I learned something from this list. Rob Dixon also pointed out some mistakes in my coding also. Which is better to use: 1) my ( $FTPOBJ ) = @_; --or-- 2) my $FTPOBJ = shift; I've seen both used a lot. I use 2) for a single parameter (sometimes even 2 or 3 depending on usage) and 1) for handling multiple parameters. -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /-- o // // Castle of Medieval Myth Magic http://www.todbe.com/ -/-' /___/__/_/_http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: undef example
Martin Leese wrote: Rob Dixon [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: 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. Why? To quote Programming Perl: The official name of a subroutine includes the prefix. A subroutine may be called using the prefix, but the is usually optional. When used with parentheses, the does nothing differently. Including it makes clear that the sub is user-defined. Not entirely true. From perlsub manpage : --- To call subroutines: NAME(LIST);# is optional with parentheses. NAME LIST; # Parentheses optional if predeclared/imported. NAME(LIST); # Circumvent prototypes. NAME; # Makes current @_ visible to called subroutine. --- Using the will disable the use of prototypes. You should naturally pre-declare your subs that use prototypes. A little further down : A subroutine may be called using an explicit prefix. The is optional in modern Perl, as are parentheses if the subroutine has been predeclared. The is *not* optional when just naming the subroutine, such as when it's used as an argument to defined() or undef(). Nor is it optional when you want to do an indirect subroutine call with a subroutine name or reference using the $subref() or {$subref}() constructs, although the $subref-() notation solves that problem. See perlref for more about all that. Subroutines may be called recursively. If a subroutine is called using the form, the argument list is optional, and if omitted, no @_ array is set up for the subroutine: the @_ array at the time of the call is visible to subroutine instead. This is an efficiency mechanism that new users may wish to avoid. foo(1,2,3);# pass three arguments foo(1,2,3); # the same foo(); # pass a null list foo(); # the same foo; # foo() get current args, like foo(@_) !! foo;# like foo() IFF sub foo predeclared, else foo Not only does the form make the argument list optional, it also disables any prototype checking on arguments you do provide. This is partly for historical reasons, and partly for having a convenient way to cheat if you know what you're doing. See Prototypes below. -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /-- o // // Castle of Medieval Myth Magic http://www.todbe.com/ -/-' /___/__/_/_http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: undef example
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
RE: undef example
$Bill Luebkert [EMAIL PROTECTED] wrote: Using the will disable the use of prototypes. You should naturally pre-declare your subs that use prototypes. Doh! (Note to self: wait a little while longer before replying as often all will be made clear.) I actually did remember this back in the tiny recesses of my brain. This is the reason why I rarely use the symbol -- because I almost always use function prototypes. So all that remains is to hear why not to use function prototypes. (Something tells me I should wait before sending this -- augh, my first paragr.. ENDOFLINE ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: undef example
[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 ); . . __END__ sub closeFtpConn ($) { my ( $FTPOBJ ) = @_; . $FTPOBJ-quit; . . undef $FTPOBJ; } Try something like : use strict; use Net::FTP; my $mvsFtpHost = 'wherever.com'; my $MVSFTP = Net::FTP-new($mvsFtpHost, Debug = 1) or die Cannot connect to $mvsFtpHost;; closeFtpConn (\$MVSFTP); sub closeFtpConn { my $FTPOBJ = shift; $$FTPOBJ-quit; undef $$FTPOBJ; } __END__ -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /-- o // // Castle of Medieval Myth Magic http://www.todbe.com/ -/-' /___/__/_/_http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs