Kevin Pfeiffer wrote:
>
> In article <[EMAIL PROTECTED]>, Rob Dixon wrote:
>
> > Kevin Pfeiffer wrote:
> >>
> >> I would have thought that this would initialize my $indent variable to 2
> >> (like setting an initial state for an object), but if I call "indent()" I
> >> get nothing back. :-(
> [...]
> > Well yes, it will, but only when you execute it. Take a look
> > at the program below.
> [...]
> > use strict;
> > use warnings;
> >
> > printf "%d %d %d\n", indent(1), indent(2), indent(3);
> >
> > {
> > my $indent = 2;
> >
> > sub indent {
> > my $increment = shift;
> > $indent += $increment if $increment;
> > return $indent;
> > }
> > }
> >
> > printf "%d %d %d\n", indent(1), indent(2), indent(3);
> >
> > ** OUTPUT **
> >
> > 1 3 6
> > 3 5 8
>
> Hmmm, I guess I would have to move it up or add a "BEGIN" label.
I would write it as
{
my $indent;
sub indent {
my $increment = shift;
$indent = 2 unless defined $indent;
$indent += $increment if $increment;
return $indent;
}
}
Cheers,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]