Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

This is an issue that comes up a lot. Perl inherits a lot of syntax and style from C and various other languages so some of the conventions used in older Perl derive from what was required in other languages. However Perl has moved on and there are now often clearer and safer ways of doing things. As mentioned in my previous reply there are several issues related to using old school two parameter open and bare word style file handle identifiers (IN in your example). There are two main issues with using bare word file handles: it is easy to mistype them and introduce subtle and hard to find bugs, and bare word file handles have global scope.

The typo issue should be fairly obvious. The global scope thing is less so and understanding depends on understanding the difference between package variables and lexical variables. Lexical variables are the ones you introduce using my and they are pretty much limited to the block they are introduced in. For a file handle that means that the file is closed for you when processing exits the block containing the lexical variable. Consider:

for my $fileIdx (1 .. 10) { my $logName = "log$fileIdx.txt"; open my $logOut, '>', $logName or die "Can't create $logName: $!\ +n"; ... }

which creates 10 log files. Notice that a close isn't needed because $logOut goes out of scope at the closing } and Perl closes the file for you.

The three parameter open issue is also two fold. The first thing is that two parameter open defaults to input so it is fairly easy to forget that you need to provide the > to specify output - somehow that is a really hard bug to spot! The second, and more subtle issue is that a user supplied file name can run a command supplied by the user by appending a pipe ('|') character to the command and presenting that as the file name.

Note by the way that the file name is not the file handle. open creates a file handle using the file name and mode information. The file handle is just the way the functions which deal with the file for I/O know which file to manipulate and what operations are OK.

True laziness is hard work

In reply to Re^3: Issue splitting in while Loop by GrandFather
in thread Issue splitting in while Loop by savem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-03-28 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found