=head2 Tying Scalars
A class implementing a tied scalar should define the following methods:
-TIESCALAR, FETCH, STORE, and possibly DESTROY.
+TIESCALAR, FETCH, STORE, and possibly UNTIE and/or DESTROY.
Let's look at each in turn, using as an example a tie class for
scalars that allows the user to do something like:
return $new_nicety;
}
+=item UNTIE this
+
+This method will be triggered when the C<untie> occurs. This can be useful
+if the class needs to know when no further calls will be made. (Except DESTROY
+of course.) See below for more details.
+
=item DESTROY this
This method will be triggered when the tied variable needs to be destructed.
=head2 Tying Arrays
A class implementing a tied ordinary array should define the following
-methods: TIEARRAY, FETCH, STORE, FETCHSIZE, STORESIZE and perhaps DESTROY.
+methods: TIEARRAY, FETCH, STORE, FETCHSIZE, STORESIZE and perhaps UNTIE and/or DESTROY.
FETCHSIZE and STORESIZE are used to provide C<$#array> and
equivalent C<scalar(@array)> access.
methods above. The default implementations of DELETE and EXISTS in
B<Tie::Array> simply C<croak>.
-In addition EXTEND will be called when perl would have pre-extended
+In addition EXTEND will be called when perl would have pre-extended
allocation in a real array.
This means that tied arrays are now I<complete>. The example below needs
return $self->{ARRAY}[$idx];
}
-If a negative array index is used to read from an array, the index
+If a negative array index is used to read from an array, the index
will be translated to a positive one internally by calling FETCHSIZE
-before being passed to FETCH.
-
+before being passed to FETCH.
+
As you may have noticed, the name of the FETCH method (et al.) is the same
for all accesses, even though the constructors differ in names (TIESCALAR
vs TIEARRAY). While in theory you could have the same class servicing
}
return $self->{ARRAY}[$idx] = $value;
}
-
-Negative indexes are treated the same as with FETCH.
+
+Negative indexes are treated the same as with FETCH.
+
+=item UNTIE this
+
+Will be called when C<untie> happens. (See below.)
=item DESTROY this
reports whether a key is present in the hash, and DELETE deletes one.
CLEAR empties the hash by deleting all the key and value pairs. FIRSTKEY
and NEXTKEY implement the keys() and each() functions to iterate over all
-the keys. And DESTROY is called when the tied variable is garbage
-collected.
+the keys. UNTIE is called when C<untie> happens, and DESTROY is called when
+the tied variable is garbage collected.
If this seems like a lot, then feel free to inherit from merely the
standard Tie::Hash module for most of your methods, redefining only the
return each %{ $self->{LIST} }
}
+=item UNTIE this
+
+This is called when C<untie> occurs.
+
=item DESTROY this
This method is triggered when a tied hash is about to go out of
A class implementing a tied filehandle should define the following
methods: TIEHANDLE, at least one of PRINT, PRINTF, WRITE, READLINE, GETC,
-READ, and possibly CLOSE and DESTROY. The class can also provide: BINMODE,
+READ, and possibly CLOSE, UNTIE and DESTROY. The class can also provide: BINMODE,
OPEN, EOF, FILENO, SEEK, TELL - if the corresponding perl operators are
used on the handle.
sub CLOSE { print "CLOSE called.\n" }
+=item UNTIE this
+
+As with the other types of ties, this method will be called when C<untie> happens.
+It may be appropriate to "auto CLOSE" when this occurs.
+
=item DESTROY this
As with the other types of ties, this method will be called when the
have not been flushed to disk.
Now that you know what the problem is, what can you do to avoid it?
-Well, the good old C<-w> flag will spot any instances where you call
+Prior to the introduction of the optional UNTIE method the only way
+was the good old C<-w> flag. Which will spot any instances where you call
untie() and there are still valid references to the tied object. If
the second script above this near the top C<use warnings 'untie'>
or was run with the C<-w> flag, Perl prints this
undef $x;
untie $fred;
+Now that UNTIE exists the class designer can decide which parts of the
+class functionality are really associated with C<untie> and which with
+the object being destroyed. What makes sense for a given class depends
+on whether the inner references are being kept so that non-tie-related
+methods can be called on the object. But in most cases it probably makes
+sense to move the functionality that would have been in DESTROY to the UNTIE
+method.
+
+If the UNTIE method exists then the warning above does not occur. Instead the
+UNTIE method is passed the count of "extra" references and can issue its own
+warning if appropriate. e.g. to replicate the no UNTIE case this method can
+be used:
+
+ sub UNTIE
+ {
+ my ($obj,$count) = @_;
+ carp "untie attempted while $count inner references still exist" if $count;
+ }
+
=head1 SEE ALSO
See L<DB_File> or L<Config> for some interesting tie() implementations.
Tom Christiansen
TIEHANDLE by Sven Verdoolaege <F<skimo@dns.ufsia.ac.be>> and Doug MacEachern <F<dougm@osf.org>>
+
+UNTIE by Nick Ing-Simmons <F<nick@ing-simmons.net>>
+
* --jhi */
# ifdef __hpux__
/* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
- * and another MAXINT from "perl.h" <- <sys/param.h>. */
+ * and another MAXINT from "perl.h" <- <sys/param.h>. */
# undef MAXINT
# endif
# include <shadow.h>
# include <unistd.h>
#endif
-#ifdef HAS_SYSCALL
-#ifdef __cplusplus
+#ifdef HAS_SYSCALL
+#ifdef __cplusplus
extern "C" int syscall(unsigned long,...);
#endif
#endif
# include <sys/socket.h>
# if defined(USE_SOCKS) && defined(I_SOCKS)
# include <socks.h>
-# endif
+# endif
# ifdef I_NETDB
# include <netdb.h>
# endif
if (MAXARG > 1)
discp = POPs;
- gv = (GV*)POPs;
+ gv = (GV*)POPs;
if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
PUSHMARK(SP);
if (!(io = GvIO(gv)) || !(fp = IoIFP(io)))
RETPUSHUNDEF;
- if (do_binmode(fp,IoTYPE(io),mode_from_discipline(discp)))
+ if (do_binmode(fp,IoTYPE(io),mode_from_discipline(discp)))
RETPUSHYES;
else
RETPUSHUNDEF;
PUSHs(*MARK++);
PUTBACK;
call_method(methname, G_SCALAR);
- }
+ }
else {
/* Not clear why we don't call call_method here too.
* perhaps to get different error message ?
stash = gv_stashsv(*MARK, FALSE);
if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
DIE(aTHX_ "Can't locate object method \"%s\" via package \"%s\"",
- methname, SvPV(*MARK,n_a));
+ methname, SvPV(*MARK,n_a));
}
ENTER;
PUSHSTACKi(PERLSI_MAGIC);
SV *obj = SvRV(mg->mg_obj);
GV *gv;
CV *cv = NULL;
- if (ckWARN(WARN_UNTIE)) {
- if (mg && SvREFCNT(obj) > 1)
- Perl_warner(aTHX_ WARN_UNTIE,
- "untie attempted while %"UVuf" inner references still exist",
- (UV)SvREFCNT(obj) - 1 ) ;
- }
if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
isGV(gv) && (cv = GvCV(gv))) {
PUSHMARK(SP);
XPUSHs(SvTIED_obj((SV*)gv, mg));
+ XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
PUTBACK;
ENTER;
call_sv((SV *)cv, G_VOID);
LEAVE;
SPAGAIN;
}
+ else if (ckWARN(WARN_UNTIE)) {
+ if (mg && SvREFCNT(obj) > 1)
+ Perl_warner(aTHX_ WARN_UNTIE,
+ "untie attempted while %"UVuf" inner references still exist",
+ (UV)SvREFCNT(obj) - 1 ) ;
+ }
}
sv_unmagic(sv, how);
RETPUSHYES;
}
if (sv_isobject(TOPs)) {
- sv_unmagic((SV *) hv, 'P');
+ sv_unmagic((SV *) hv, 'P');
sv_magic((SV*)hv, TOPs, 'P', Nullch, 0);
}
LEAVE;
PP(pp_tell)
{
djSP; dTARGET;
- GV *gv;
+ GV *gv;
MAGIC *mg;
if (MAXARG == 0)
len = (Off_t)POPi;
#endif
/* Checking for length < 0 is problematic as the type might or
- * might not be signed: if it is not, clever compilers will moan. */
+ * might not be signed: if it is not, clever compilers will moan. */
/* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
SETERRNO(0,0);
#if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
PerlIO_flush(IoIFP(GvIOp(tmpgv)));
#ifdef HAS_TRUNCATE
if (ftruncate(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
-#else
+#else
if (my_chsize(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
#endif
result = 0;
retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
#else
retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
-#endif
+#endif
#else
DIE(aTHX_ "fcntl is not implemented");
#endif
if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
!memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
sizeof(u_short) + sizeof(struct in_addr))) {
- goto nuts2;
+ goto nuts2;
}
}
#endif
PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
# endif
#endif
-#if Gid_t_size > IVSIZE
+#if Gid_t_size > IVSIZE
PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
#else
# if Gid_t_sign <= 0
break;
}
#ifdef EBCDIC
- else if (!(isPRINT(*s) || isSPACE(*s)))
+ else if (!(isPRINT(*s) || isSPACE(*s)))
odd++;
#else
else if (*s & 128) {
PP(pp_wait)
{
-#if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
+#if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
djSP; dTARGET;
Pid_t childpid;
int argflags;
PP(pp_waitpid)
{
-#if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
+#if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
djSP; dTARGET;
Pid_t childpid;
int optype;
#if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
I32 which = PL_op->op_type;
register char **elem;
- register SV *sv;
+ register SV *sv;
#ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
struct protoent *PerlSock_getprotobyname(Netdb_name_t);
struct protoent *PerlSock_getprotobynumber(int);
register SV *sv;
STRLEN n_a;
struct passwd *pwent = NULL;
- /*
+ /*
* We currently support only the SysV getsp* shadow password interface.
* The interface is declared in <shadow.h> and often one needs to link
* with -lsecurity or some such.
*
* Note that <sys/security.h> is already probed for, but currently
* it is only included in special cases.
- *
+ *
* In Digital UNIX/Tru64 if using the getespw*() (which seems to be
* be preferred interface, even though also the getprpw*() interface
* is available) one needs to link with -lsecurity -ldb -laud -lm.
a[i++] = SvIV(*MARK);
else if (*MARK == &PL_sv_undef)
a[i++] = 0;
- else
+ else
a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
if (i > 15)
break;
}
#ifdef FCNTL_EMULATE_FLOCK
-
+
/* XXX Emulate flock() with fcntl().
What's really needed is a good file locking module.
*/
fcntl_emulate_flock(int fd, int operation)
{
struct flock flock;
-
+
switch (operation & ~LOCK_NB) {
case LOCK_SH:
flock.l_type = F_RDLCK;
}
flock.l_whence = SEEK_SET;
flock.l_start = flock.l_len = (Off_t)0;
-
+
return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
}