Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Diamond or Open, Process, Close?

by heatblazer (Scribe)
on Sep 27, 2012 at 10:39 UTC ( #995973=perlquestion: print w/replies, xml ) Need Help??
heatblazer has asked for the wisdom of the Perl Monks concerning the following question:

Hello, everybody.

I am still understanding Perl, but what bothers me some examples with open filehandles closing them and the diamond operator with the special $^I variable. What bothers me: why using open for read and write when we can use <> ? The diamod did the better job for me actually.

Update on 28.09.2012

I`ve found an excersise in Lama book on page 168 in which as an extra credit at 4 and 5, I think was the using of the diamond operator. Solving these two ex-es, I`ve found out that diamond has some better feats than open/close ( for text files since you always read and write ( consider you`ve used the -i variable ), however you can miss the --rwx check of the file and try to process a root file or unreadable/writable one. However I think that both are useful for different tasks, I think I`ve solved my mystery about the diamond operator, also I was wondering ( by my assumption the answer is 'NO') if diamond can fseek the file or you can just match with __LINE__ variable... Open/close still puts a fpointer to the file while diamond is some kind of magic using a copy and original files... Maybe this is a strong point if you worry about losing a file. And... I doubt that diamond can open db and binary files, it`s just my guess, I may fail at it too.

Replies are listed 'Best First'.
Re: Diamond or Open, Process, Close?
by CountZero (Bishop) on Sep 27, 2012 at 12:37 UTC
    Because open can be used everywhere and <> works only in special cases?

    What will you do when you need to read from two files and write to a third?


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Diamond or Open, Process, Close?
by bart (Canon) on Sep 27, 2012 at 10:56 UTC
    Perl's -i mode creates a new file and then renames the new file as the old file. That might give you problems if you are not the owner of the file, as both file owner and modes may, and likely will, change.

    Besides, open (or sysopen) is the low level operator that perl uses internally. What you are asking is similar to asking "Should I write my own code, or use a module?" except that here, the "module" (the implementation of the <> operator) is built into Perl itself.

Re: Diamond or Open, Process, Close?
by Anonymous Monk on Sep 27, 2012 at 10:43 UTC

    Because it looks funny? Its perl4-ism? Its magic (Dangerous diamonds!)?

    Don't be bothered by clarity and verbosity

      Kind of... real magic??? ^_^

Re: Diamond or Open, Process, Close?
by daxim (Chaplain) on Sep 27, 2012 at 10:44 UTC
    Can you give code examples with open/close that bothers you so we can see what you mean?

      No, I don`t have a particular code, but the <> just did the same job without bothering opening and closing files, so I wonder - what the?! Or it`s jsut another TIMTOWDI magic?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995973]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2017-05-25 09:05 GMT
Find Nodes?
    Voting Booth?