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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to tied file handle, extra variable? by live4tech

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (5)
    As of 2018-02-21 19:50 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (287 votes). Check out past polls.