Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Nested While loop not working

by vinoth.ree (Parson)
on Mar 11, 2014 at 09:19 UTC ( #1077820=note: print w/ replies, xml ) Need Help??


in reply to Nested While loop not working

As choroba suggested, use three argument open() with die

There are two forms of the open() function in Perl 5. The modern version takes three arguments: the filehandle to open or vivify, the mode of the filehandle, and the name of the file.

The legacy version has two arguments, only the filehandle and the name of the file. The mode of the file comes from the filename; if the filename starts (or ends) with any of several special characters, open() parses them off and uses them.

If you accidentally use a filename with those special characters with the two-arg form of open(), your code will not behave as you expect. This is especially a problem if you are not careful about sanitizing user input, and if any user input ever becomes part of a filename. Ex.

open my $fh, ">$filename" # INSECURE CODE; do not use or die "Can't write to '$filename': $!\n";
While this code appears to open $filename for writing, an insecure $filename could start with > to force appending mode, or - to open STDOUT. Likewise, code without any explicit mode in the second and final parameter is susceptible to any special mode characters.

Extracting file modes into a separate parameter to this function prevents Perl from parsing the filename at all and removes the possibility for this unintentional behavior. As Damian Conway has mentioned, using a separate file mode parameter also makes the intention of the code clearer

open my $fh, '>', $filename # safer and clearer or die "Can't write to '$filename': $!\n";

The modern version of this code is safer and clearer.


All is well


Comment on Re: Nested While loop not working
Select or Download Code
Re^2: Nested While loop not working
by Manisha (Initiate) on Mar 11, 2014 at 09:40 UTC

    Hi Vinoth, I used the three arg open syntax but it doesnt help ..

      What happens?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1077820]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-07-29 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls