static variable problem

Discussion in 'Perl Misc' started by Dave Saville, Nov 13, 2012.

  1. Dave Saville

    Dave Saville Guest

    Please why does this throw an error?

    [T:\tmp]cat try.pl
    use strict;
    use warnings;

    foo("Hello World");
    exit;
    {
    my $bar = 1;

    sub foo
    {
    my $a = shift;
    print "$bar $a\n"; # <== line 12
    return;
    }
    }

    [T:\tmp]try.pl
    Use of uninitialized value in concatenation (.) or string at try.pl
    line 12.
    Hello World

    So $bar is private to foo - but you can't initialise it?

    TIA
    --
    Regards
    Dave Saville
    Dave Saville, Nov 13, 2012
    #1
    1. Advertising

  2. "Dave Saville" <> writes:
    > Please why does this throw an error?
    >
    > [T:\tmp]cat try.pl
    > use strict;
    > use warnings;
    >
    > foo("Hello World");
    > exit;
    > {
    > my $bar = 1;
    >
    > sub foo
    > {
    > my $a = shift;
    > print "$bar $a\n"; # <== line 12
    > return;
    > }
    > }
    >
    > [T:\tmp]try.pl
    > Use of uninitialized value in concatenation (.) or string at try.pl
    > line 12.
    > Hello World


    Because $bar = 1 is never executed: The compiler compiles this, then
    the program invokes the foo subroutine and exits before the $bar = 1.
    You can move the foo("Hello World") below the block or use BEGIN or
    INIT to ensure that $bar is actually initialized:

    ----------
    use strict;
    use warnings;

    foo("Hello World");
    exit;
    {
    my $bar;

    INIT {
    $bar = 1;
    }

    sub foo
    {
    my $a = shift;
    print "$bar $a\n"; # <== line 12
    return;
    }
    }
    Rainer Weikusat, Nov 13, 2012
    #2
    1. Advertising

  3. Rainer Weikusat <> writes:
    > "Dave Saville" <> writes:
    >> Please why does this throw an error?
    >>
    >> [T:\tmp]cat try.pl
    >> use strict;
    >> use warnings;
    >>
    >> foo("Hello World");
    >> exit;
    >> {
    >> my $bar = 1;
    >>
    >> sub foo
    >> {
    >> my $a = shift;
    >> print "$bar $a\n"; # <== line 12
    >> return;
    >> }
    >> }
    >>
    >> [T:\tmp]try.pl
    >> Use of uninitialized value in concatenation (.) or string at try.pl
    >> line 12.
    >> Hello World

    >
    > Because $bar = 1 is never executed: The compiler compiles this, then
    > the program invokes the foo subroutine and exits before the $bar =
    > 1.


    Additional explanation: Generally, running a Perl program is comprised
    of two phases: A compilation step where the Perl compiler reads all
    the source code files and transforms the code into some internal
    representation (AFAIK called 'an optree') and an execution phase where
    the Perl interpreter evaluates/ executes this 'optree'. This means any
    statement in a Perl program may have run time or compile time effects
    or both and the run time effects aren't visible during compile
    time. For this example, this means the compiler creates a compiled
    subroutine 'foo' which refers to the $bar defined in the outer block
    because 'creating my variables' has the compile-time effect of making
    the corresponding names visible to the compiler (so that it can
    generated code accessing the proper objects). But the assignment to
    $bar does not happen at compile-time, it happens at run-time when the
    corresponding statement is executed. An execution of the 'content' of
    a file is top-to-bottom: By the time the foo("...:") is executed, a
    subroutine referencing the proper $bar has been created but the value
    of $bar hasn't yet been set.

    So-called 'BEGIN block' can be used to execute Perl code at
    compile-time and alternatively, so-called 'INIT blocks' can be used to
    run Perl code after the compilation phase has finished and before any
    of the 'general' code ran (details are in the perlmod manpage).
    Rainer Weikusat, Nov 13, 2012
    #3
  4. Dave Saville

    C.DeRykus Guest

    On Tuesday, November 13, 2012 5:38:07 AM UTC-8, Dave Saville wrote:
    > Please why does this throw an error?
    >
    >
    >
    > [T:\tmp]cat try.pl
    >
    > use strict;
    >
    > use warnings;
    >
    >
    >
    > foo("Hello World");
    >
    > exit;
    >
    > {
    >
    > my $bar = 1;
    >
    >
    >
    > sub foo
    >
    > {
    >
    > my $a = shift;
    >
    > print "$bar $a\n"; # <== line 12
    >
    > return;
    >
    > }
    >
    > }
    >
    >
    >
    > [T:\tmp]try.pl
    >
    > Use of uninitialized value in concatenation (.) or string at try.pl
    >
    > line 12.
    >
    > Hello World
    >
    >
    >
    > So $bar is private to foo - but you can't initialise it?
    >
    >


    And just for a spaghetti western :)


    goto BLOCK;

    FOO: foo("Hello World");
    exit;

    BLOCK: {
    my $bar = 1;

    sub foo
    {
    my $a = shift;
    print "$bar $a\n";
    return;
    }
    goto FOO;
    };
    __END__

    [ duck ] C.DeRykus
    C.DeRykus, Nov 13, 2012
    #4
  5. Perl goto (was: static variable problem)

    "C.DeRykus" <> writes:

    [...]

    > goto BLOCK;
    >
    > FOO: foo("Hello World");
    > exit;
    >
    > BLOCK: {
    > my $bar = 1;
    >
    > sub foo
    > {
    > my $a = shift;
    > print "$bar $a\n";
    > return;
    > }
    > goto FOO;
    > };


    In contrast to 'other languages', the destination of a Perl goto is
    determined at runtime, by 'outward' searching (IIRC, term used by the
    Camel book) for a matching label in all currently active lexical
    scopes. This implies that, compared to other means for performing flow
    control, it is a very expensive operation.

    --------------
    use Benchmark;

    timethese(-5,
    {
    goto => sub {
    goto out;

    print "ha!";

    out:
    return 3;
    },

    return => sub {
    return 3;
    }});
    Rainer Weikusat, Nov 14, 2012
    #5
  6. Dave Saville

    C.DeRykus Guest

    Re: Perl goto (was: static variable problem)

    On Wednesday, November 14, 2012 5:23:49 AM UTC-8, Rainer Weikusat wrote:
    > "C.DeRykus" <> writes:
    >
    >
    >
    > [...]
    >
    >
    >
    > > goto BLOCK;

    >
    > >

    >
    > > FOO: foo("Hello World");

    >
    > > exit;

    >
    > >

    >
    > > BLOCK: {

    >
    > > my $bar = 1;

    >
    > >

    >
    > > sub foo

    >
    > > {

    >
    > > my $a = shift;

    >
    > > print "$bar $a\n";

    >
    > > return;

    >
    > > }

    >
    > > goto FOO;

    >
    > > };

    >
    >
    >
    > In contrast to 'other languages', the destination of a Perl goto is
    >
    > determined at runtime, by 'outward' searching (IIRC, term used by the
    >
    > Camel book) for a matching label in all currently active lexical
    >
    > scopes. This implies that, compared to other means for performing flow
    >
    > control, it is a very expensive operation.
    >
    >
    >
    > --------------
    >
    > use Benchmark;
    >
    >
    >
    > timethese(-5,
    >
    > {
    >
    > goto => sub {
    >
    > goto out;
    >
    >
    >
    > print "ha!";
    >
    >
    >
    > out:
    >
    > return 3;
    >
    > },
    >
    >
    >
    > return => sub {
    >
    > return 3;
    >
    > }});


    Thanks, that's interesting. I would have
    thought that it'd be much cheaper. Of course,
    spaghetti code is super bad anyway for all
    the reasons we know about. You gotta have
    Clint Eastwood's steely gaze and grit to
    follow the crooked trails and dodge bullets
    that might ricochet anywhere.

    That's why I said "duck".

    --
    Charles DeRykus
    C.DeRykus, Nov 14, 2012
    #6
  7. Dave Saville

    Guest

    Thanks all. I was a bit confused because my test was pasted straight out of perldocs. :)

    Sorry for the late response but I am having problems with my newsreader and never saw my own post nor any of the replies. Had to log into Google to send this - Yuk I hate web groups.
    , Nov 15, 2012
    #7
  8. writes:
    > Thanks all. I was a bit confused because my test was pasted straight
    > out of perldocs. :)
    >
    > Sorry for the late response but I am having problems with my
    > newsreader and never saw my own post nor any of the replies. Had to
    > log into Google to send this - Yuk I hate web groups.


    Where exactly in the Perl docs did you find that code?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 15, 2012
    #8
    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. kookey
    Replies:
    3
    Views:
    20,736
    Andrew Thompson
    Aug 20, 2005
  2. Patrick Hoffmann
    Replies:
    3
    Views:
    2,811
    Christian Jan├čen
    Aug 8, 2003
  3. Marcin Vorbrodt

    Global static variable vs static method

    Marcin Vorbrodt, Sep 5, 2003, in forum: C++
    Replies:
    3
    Views:
    5,402
    Denis Perelyubskiy
    Sep 5, 2003
  4. Sam
    Replies:
    4
    Views:
    457
    The Directive
    Jan 13, 2004
  5. Vikram Kalra
    Replies:
    23
    Views:
    861
    Nigel Wade
    Sep 7, 2007
Loading...

Share This Page