Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

perl handle and common variable

by anaconda_wly (Scribe)
on Apr 08, 2013 at 05:55 UTC ( #1027430=perlquestion: print w/replies, xml ) Need Help??
anaconda_wly has asked for the wisdom of the Perl Monks concerning the following question:

below is the way creating a file a write. open report_handler, ">", "report.txt"; Can I change the file handle "report_handler" to a variable starting with $?
my $report_handler; open $report_handler, ">", "report.txt";
I tried and seems OK, so why we need the file handle?

Replies are listed 'Best First'.
Re: perl handle and common variable
by aitap (Deacon) on Apr 08, 2013 at 07:03 UTC
Re: perl handle and common variable
by dsheroh (Prior) on Apr 08, 2013 at 09:48 UTC
    Just to clarify some terminology that it appears you may have misunderstood: At the end of your second bit of posted code, $report_handler is a file handle. A "file handle" is the operating-system level resource used by programs to interact with files (and other file-like things, like pipes, stdin, stdout, etc.).

    The difference you've discovered is the difference between a bareword (or typeglob) file handle (report_handler) and a lexical file handle ($report_handler). Bareword file handles mostly exist for the sake of backwards compatibility these days. Any new code should use lexical file handles instead.

Re: perl handle and common variable
by Anonymous Monk on Apr 08, 2013 at 06:00 UTC

    I tried and seems OK, so why we need the file handle?

    Because you didn't read the documentation for open or perlintro?

Re: perl handle and common variable
by Rahul6990 (Beadle) on Apr 08, 2013 at 06:10 UTC
    File handle is required to perform read and write operation on the underlying file. For accessing the file content you have to go through the file handle. Example: $file = '/etc/passwd'; # Name the file open(INFO, $file); # Open the file @lines = <INFO>; # Read it into an array close(INFO); # Close the file print @lines; # Print the array

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027430]
Approved by Corion
[MidLifeXis]: Most likely it is a code that some undocumented system, hidden behind layers of IT, deep in the bowels of the building under the machine room floor, reads that code to keep a presence switch from going off. :-b
[MidLifeXis]: I think I forgot "running on a farm of commodore 64, vic 20s, trs 80s, and apple ]|[e systems"
[GotToBTru]: oh I know what it is .. but it is a number only slightly useful to me and of no possible use to our customer
[MidLifeXis]: Whew - you just saved the free world. <o)
[GotToBTru]: i guess it's a placeholder, the code will only fill it in if there is nothing else to use
[GotToBTru]: but then .. if you have nothing to say, why not say nothing?
[MidLifeXis]: I have a user who has a lot of say on how some of our processes work that abhors significant blanks. Perhaps that is a part of it. A not-so-obvious "this space intentionally left blank" indicator.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2017-01-20 19:11 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (176 votes). Check out past polls.