Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

tied file handle, extra variable?

by live4tech (Sexton)
on Jan 06, 2013 at 08:22 UTC ( #1011847=perlquestion: print w/replies, xml ) Need Help??
live4tech has asked for the wisdom of the Perl Monks concerning the following question:

I wish a good start to the new year to everyone!

I have been reading Damian Conway's Object Oriented Perl book. I have enjoyed it for the most part especially the beginning chapters where I think he does a good job of covering some perl basics. After that, the book is a bit of a struggle probably because I am new to perl and because this book is from 2000 and some of it is definitely out of date. In any case the following code has me perplexed:

sub TIEHANDLE { my ($class, %args) = @_; my $handle = gensym(); my $impl = bless { handle => gensym() }, $class; $impl->OPEN (%args); return $impl; } sub OPEN { my ($impl, %args) = @_; open $impl->{handle}, $args{file} or croak "Could not open '$args{f +ile}'"; $impl->{in_filter} = $args{in} || \&_no_filter; $impl->{out_filter} = $args{out} || \&_no_filter; }

I am confused by something in the subroutine TIEHANDLE. Here is what I think is going on:

The class name ($class) is taken from the list of parameters passed to the subroutine. A hash (%args) is taken from the rest of the parameter list.

Then $handle is assigned to an anonymous glob and therefore becomes a reference to a type glob.

Then $impl is initialized as an object (blessed into the package) referencing an anonymous hash with a key "handle" and value gensym() (creating another reference to a glob).

Then the method OPEN is called using $impl->OPEN (%args)

The $impl object is then returned.

My question is, why do we need the reference "$handle" in the line "my $handle = gensym()"? This reference is not used after this line. In fact a new glob reference is created in the next line in the hash which is blessed. Why do we need $handle at all here? Also I have read the gensym() perldoc and it looks like it is not used anymore although that document is difficult to understand.

Replies are listed 'Best First'.
Re: tied file handle, extra variable?
by BrowserUk (Pope) on Jan 06, 2013 at 08:51 UTC
    Why do we need $handle at all here?

    You don't. Its a typo/thinko/edito.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: tied file handle, extra variable?
by Anonymous Monk on Jan 06, 2013 at 09:19 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1011847]
Approved by BrowserUk
help
Chatterbox?
[stevieb]: I just received full professional license for ALL of JetBrains software due to requesting their opensource request form! I wanted only CLion, but they give them all
[stevieb]: I stated I use IDEA for Perl work, and at work, licensed version of PyCharm
[beech]: is that a good thing?
[stevieb]: absolutely. Free professional IDEs and related software for all the langs I write in, and I wanted CLion because it's a C IDE that can communicate directly with an Arduino

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2017-02-20 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (302 votes). Check out past polls.