2 examples of packages 1 has error the other does not

Discussion in 'Perl Misc' started by Billy Patton, Jun 6, 2007.

  1. Billy Patton

    Billy Patton Guest

    I'm writing and testing a module that can be executed as a standalone script
    for debugging purposes.
    It's part of a very large system of regression testing. Messages are lost
    along the way.
    So with the caller() function I transform a module into an executable.
    With example #1 my $x seems to have lost it's scope.

    What I'm worried about, is what is goinf to happen when in the big system it
    does a :
    use foo;
    or
    require "foo.pm";

    example #1
    #!/usr/local/bin/perl
    use strict;
    use warnings;
    if ( !defined caller ) { foo::bar();}
    package foo;
    my $x = 'foo.bar';
    sub bar { print "$x\n"; } <<<<------ line 13 blank lines removed
    1;

    executing gives this :
    Use of uninitialized value in concatenation (.) or string at z line 13.


    example #2 works fine
    #!/usr/local/bin/perl
    package foo;
    use strict;
    use warnings;
    my $x = 'foo.bar';
    sub bar { print "$x\n"; }
    if ( !defined caller ) {
    # enter package main
    package main;
    use strict;
    use warnings;
    foo::bar();
    } # leave package main
    1;
     
    Billy Patton, Jun 6, 2007
    #1
    1. Advertising

  2. Billy Patton

    Mumia W. Guest

    On 06/06/2007 12:40 PM, Billy Patton wrote:
    > I'm writing and testing a module that can be executed as a standalone script
    > for debugging purposes.
    > It's part of a very large system of regression testing. Messages are lost
    > along the way.
    > So with the caller() function I transform a module into an executable.
    > With example #1 my $x seems to have lost it's scope.
    >
    > What I'm worried about, is what is goinf to happen when in the big system it
    > does a :
    > use foo;
    > or
    > require "foo.pm";
    >
    > example #1
    > #!/usr/local/bin/perl
    > use strict;
    > use warnings;
    > if ( !defined caller ) { foo::bar();}
    > package foo;
    > my $x = 'foo.bar';
    > sub bar { print "$x\n"; } <<<<------ line 13 blank lines removed
    > 1;
    >
    > executing gives this :
    > Use of uninitialized value in concatenation (.) or string at z line 13.
    >
    > [...]


    Move the line that tests caller() and calls foo::bar() down below the
    line that sets $x. Right now, when foo::bar() is invoked, $x has not
    been given any value, so it's undefined.
     
    Mumia W., Jun 7, 2007
    #2
    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. Paul  Smith
    Replies:
    0
    Views:
    734
    Paul Smith
    Nov 18, 2003
  2. praba kar

    reg packages examples

    praba kar, Aug 23, 2005, in forum: Python
    Replies:
    0
    Views:
    261
    praba kar
    Aug 23, 2005
  3. Nathan Sokalski
    Replies:
    1
    Views:
    802
    Nathan Sokalski
    Nov 28, 2007
  4. David Lyon
    Replies:
    19
    Views:
    583
    David Lyon
    Apr 24, 2009
  5. Ian
    Replies:
    3
    Views:
    171
Loading...

Share This Page