RE: confused by use of 'implied' variable
From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Greg VisionInfosoft Sent: 05 April 2013 21:16 To: Perl-Win32-Users@listserv.ActiveState.com Subject: confused by use of 'implied' variable im butchering a public script i found on the internet for purpose of doing a CGI file upload. Be very careful, there is a lot of poor quality scripts out there. theres one excerpt from the script that ive never used before. the few lines... while ( $upload_filehandle ) { print UPLOADFILE; } if it were me, i would not write code this way, i write in a way that make it easier for me to quickly understand what i was trying to do. if it were me coding this in my 'lame' (for idiots) way, it would look more like... while ( $data = $upload_filehandle ) { print UPLOADFILE $data; } Not unreasonable, but you should restrict the scope of your variable to the while loop. while (my $data = $upload_filehandle ) { etc. but now that ive seen the code, as originally presented, it does cause me to ask the question... how does one know under what set of circumstances can such 'abbreviated' code be written? in other words, how can one know what kinds of features or operations can use the implied variable @_ (which I assume is the variable that would be used in this case). The implied variable here is $_, not @_. specifically, if i wanted to append to a variable $aggregated_file_contents, each new block of the file as it was being read and output... my thought was to try: while ( $upload_filehandle ) { print UPLOADFILE; $aggregated_file_contents.=; You have to be explicit here. That is... $aggregated_file_contents .= $_; } as opposed to what i would have normally done ($aggregated_file_contents .= $data;) yes, i know i can simply try it, to see if it works - but is there a more general rule one can follow that tells them when this kind of thing can normally be used, versus when not? Start with 'perldoc perlvar'. It is the first one listed under General Variables. HTH -- Brian Raven Please consider the environment before printing this e-mail. This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
confused by use of 'implied' variable
im butchering a public script i found on the internet for purpose of doing a CGI file upload. theres one excerpt from the script that ive never used before. the few lines... while ( $upload_filehandle ) { print UPLOADFILE; } if it were me, i would not write code this way, i write in a way that make it easier for me to quickly understand what i was trying to do. if it were me coding this in my 'lame' (for idiots) way, it would look more like... while ( $data = $upload_filehandle ) { print UPLOADFILE $data; } but now that ive seen the code, as originally presented, it does cause me to ask the question... how does one know under what set of circumstances can such 'abbreviated' code be written? in other words, how can one know what kinds of features or operations can use the implied variable @_ (which I assume is the variable that would be used in this case). specifically, if i wanted to append to a variable $aggregated_file_contents, each new block of the file as it was being read and output... my thought was to try: while ( $upload_filehandle ) { print UPLOADFILE; $aggregated_file_contents.=; } as opposed to what i would have normally done ($aggregated_file_contents .= $data;) yes, i know i can simply try it, to see if it works - but is there a more general rule one can follow that tells them when this kind of thing can normally be used, versus when not? thanks alot for any insights here greg ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: confused by use of 'implied' variable
Greg -- The shortcuts us the perl variable $_. That is, when no specific argument is supplied, $_ is used by default. s/x/y/; while ( ) {...} print; All the above and more expect a value to work with, and if none is given, perl uses $_ by default. You get into trouble when you have a loop that refers to $_ which then calls a subroutine that does something else with $_... See http://perldoc.perl.org/perlvar.html#General-Variables for details on $_. Your $aggregated_file_contents example wouldn't work, by the way: there's no default variable in that context. You'd have to do this: $aggregated_file_contents .= $_ ; Hope this helps! On Friday, April 5, 2013, Greg VisionInfosoft wrote: im butchering a public script i found on the internet for purpose of doing a CGI file upload. theres one excerpt from the script that ive never used before. the few lines... while ( $upload_filehandle ) { print UPLOADFILE; } if it were me, i would not write code this way, i write in a way that make it easier for me to quickly understand what i was trying to do. if it were me coding this in my 'lame' (for idiots) way, it would look more like... while ( $data = $upload_filehandle ) { print UPLOADFILE $data; } but now that ive seen the code, as originally presented, it does cause me to ask the question... how does one know under what set of circumstances can such 'abbreviated' code be written? in other words, how can one know what kinds of features or operations can use the implied variable @_ (which I assume is the variable that would be used in this case). specifically, if i wanted to append to a variable $aggregated_file_contents, each new block of the file as it was being read and output... my thought was to try: while ( $upload_filehandle ) { print UPLOADFILE; $aggregated_file_contents.=; } as opposed to what i would have normally done ($aggregated_file_contents .= $data;) yes, i know i can simply try it, to see if it works - but is there a more general rule one can follow that tells them when this kind of thing can normally be used, versus when not? thanks alot for any insights here greg -- Will Trillich :: 812.454.6431 “Grading takes away all the fun from failing. And a huge part of education is about failure.” -- Shimon Schocken ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs