subroutine local variable with initialization

Discussion in 'Perl Misc' started by Mark, Nov 14, 2008.

  1. Mark

    Mark Guest

    Suppose I have a program with the entry point at the top and
    subroutines following the exit point. I want to have a subroutine
    local variable, $count in my example, that is initialized to a
    starting value.

    This works, but seems overly cumbersome to me. Is there a better way?

    use strict;
    use warnings;

    # entry point
    doit();
    doit();
    exit; # exit point before subroutine definition

    {
    my $count;

    BEGIN {
    $count = 1;
    }

    sub doit {
    print "count=$count\n";
    $count++;
    }
    }

    __END__

    Output:
    count=1
    count=2
    Mark, Nov 14, 2008
    #1
    1. Advertising

  2. Mark

    Uri Guttman Guest

    >>>>> "M" == Mark <> writes:

    M> doit();
    M> exit; # exit point before subroutine definition

    M> {
    M> my $count;

    M> BEGIN {
    M> $count = 1;
    M> }

    M> sub doit {
    M> print "count=$count\n";
    M> $count++;
    M> }
    M> }

    put the sub and count together into the begin block:

    BEGIN {

    my $count = 1 ;

    sub doit {
    print "count=$count\n";
    $count++;
    }
    }

    or use closures (plenty about that on the net so google for it)

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Nov 14, 2008
    #2
    1. Advertising

  3. Mark <> writes:

    > Suppose I have a program with the entry point at the top and
    > subroutines following the exit point. I want to have a subroutine
    > local variable, $count in my example, that is initialized to a
    > starting value.
    >
    > This works, but seems overly cumbersome to me. Is there a better way?


    Some different solutions:

    * put your $count variable and initialization at the top.

    * put the whole $count and subroutine construct in a module and use() it
    (this is probably what I would prefer to do)

    * get perl 5.10 and use state() variables

    * use the my $var if 0 trick - not recommended, probably.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Nov 14, 2008
    #3
  4. Mark

    Uri Guttman Guest

    >>>>> "JD" == Joost Diepenmaat <> writes:

    JD> Some different solutions:

    JD> * use the my $var if 0 trick - not recommended, probably.

    given yours and my suggestions, let's never mention the if 0 hack again!
    it may even break in future perls as it is not needed with state vars.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Nov 14, 2008
    #4
  5. On 14 Nov, 18:17, Uri Guttman <> wrote:
    >
    > BEGIN {
    >
    >     my $count = 1 ;
    >
    >     sub doit {
    >         print "count=$count\n";
    >         $count++;
    >     }
    >
    > }


    I would argue that an INIT block would be more idiomatically correct
    rather than a BEGIN one.

    But it does not really make any difference.
    Brian McCauley, Nov 20, 2008
    #5
  6. [A complimentary Cc of this posting was NOT [per weedlist] sent to
    Brian McCauley
    <>], who wrote in article <>:
    > > BEGIN {
    > >
    > > =A0 =A0 my $count =3D 1 ;
    > >
    > > =A0 =A0 sub doit {
    > > =A0 =A0 =A0 =A0 print "count=3D$count\n";
    > > =A0 =A0 =A0 =A0 $count++;
    > > =A0 =A0 }
    > >
    > > }


    > I would argue that an INIT block would be more idiomatically correct
    > rather than a BEGIN one.


    > But it does not really make any difference.


    .... Well, if you forget that the version with BEGIN works, and the
    version with INIT works only in some situations, then yes...

    Hope this helps,
    Ilya
    Ilya Zakharevich, Nov 21, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gyan
    Replies:
    7
    Views:
    961
    Victor Bazarov
    May 19, 2006
  2. V.Subramanian, India

    initialization of local static variable

    V.Subramanian, India, Oct 7, 2011, in forum: C Programming
    Replies:
    41
    Views:
    1,949
    Tim Rentsch
    Jan 25, 2012
  3. Phil Jacobson

    local and my variables in a subroutine

    Phil Jacobson, Mar 2, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    139
    Tad McClellan
    Mar 6, 2004
  4. king
    Replies:
    5
    Views:
    178
  5. PerlFAQ Server
    Replies:
    0
    Views:
    182
    PerlFAQ Server
    Jan 12, 2011
Loading...

Share This Page