On Tue, 2004-01-13 at 17:03, A. Pagaltzis wrote:
> It would be nice if there were a way to correlate two instances
> of the variable. Along the lines of your example, I might do
> something like
>
> print "would you$please shut the door$please?";
>
> but in your current version there's a chance that this might
> print " please" twice, which would be undesired.
{ my $plz1 = $please;
my $plz2 = ( length($plz)? '' : $please )
print "would you$plz1 shut the door$plz2?";
}
That will favor the first position slightly, but they will never
both be nonempty.
q = 1-p
prob (plz1 is nonempty) = p = p
prob (plz2 is nonempty) = p * q = p - p^2
prob (both are empty) = q^2 = 1 - 2p + p^2
totals to 1
leaving the probability of additional cases at zero.
Acme::please is just sugar to prevent me having to define it
immediately before use every time I want to use it; something like
my $please = (rand(100) > $pctg ? '' : ' please');
and it might be better used in the invocation of a really simple
TIESCALAR module that simply ties the scalar to some code passed in as
an argument; that would be more flexible still --
package Tie::Scalar::To::A::Coderef;
use Carp;
sub FETCH {
&{shift()}
}
sub TIESCALAR{
ref($_[1]) eq 'CODE' or croak <<'EOF';
the Tie::Scalar::To::A::Coderef has been invoked improperly.
Here's an example of how to do it:
{
my $count = 1;
tie $Incrementor => Tie::Scalar::To::A::Coderef =>
sub { $count++ };
}
EOF
bless $_[1]
}
__END__
--
david nicol
"Rabbit troop sucks!"