Mod_perl & do()

Discussion in 'Perl Misc' started by Mike Mimic, Sep 13, 2003.

  1. Mike Mimic

    Mike Mimic Guest

    Hi!

    I tryed to use do() function in mod_perl 1.27 but as I found out that
    does not work. For example

    test.pl:
    #!/usr/bin/perl
    my $test = 0;
    do 'text.cf';
    print "Content-type: text/html\n\n";
    print $test;

    test.cf:
    $test = 1;

    prints 0 and not 1. Files are in the same directory and there is '.' in
    @INC.

    scalar eval `cat test.cf` works but I really would not like to use this.

    I use this for including configuration data.


    Mike
    Mike Mimic, Sep 13, 2003
    #1
    1. Advertising

  2. Mike Mimic

    Jay Tilton Guest

    Mike Mimic <> wrote:

    : I tryed to use do() function in mod_perl 1.27 but as I found out that
    : does not work. For example
    :
    : test.pl:
    : #!/usr/bin/perl
    : my $test = 0;

    Declaring that $test with my() gives it file scope...

    : do 'text.cf';
    : print "Content-type: text/html\n\n";
    : print $test;
    :
    : test.cf:
    : $test = 1;

    ....so that $test is a completely different variable.
    Jay Tilton, Sep 13, 2003
    #2
    1. Advertising

  3. Mike Mimic

    Mike Mimic Guest

    Hi!

    > Declaring that $test with my() gives it file scope...
    >
    > : do 'text.cf';
    > : print "Content-type: text/html\n\n";
    > : print $test;
    > :
    > : test.cf:
    > : $test = 1;
    >
    > ...so that $test is a completely different variable.


    I think that it is not true.

    do 'text.cf' should simply include code (like C++ include pragma) so
    that the code of the program would be

    #!/usr/bin/perl
    my $test = 0;
    $test = 1;
    print "Content-type: text/html\n\n";
    print $test;

    I have changed the program slightly to show the problem (test.cf is not
    loaded).

    test.pl:
    #!/usr/bin/perl
    my $test = 0;
    print "Content-type: text/html\n\n";
    do 'text.cf';
    print $test;

    test.cf:
    print "Loaded.\n";
    $test = 1;

    It prints only 0.


    Mike
    Mike Mimic, Sep 13, 2003
    #3
  4. In article <bTB8b.2563$>, Mike Mimic wrote:
    > Hi!
    >
    >> Declaring that $test with my() gives it file scope...

    [cut]
    > I think that it is not true.

    [cut]

    The manual for 'do' (perldoc -f do) says:

    [...] code evaluated with "do FILENAME" cannot see lexicals
    in the enclosing scope [...]

    --
    Andreas Kähäri
    Andreas Kahari, Sep 13, 2003
    #4
  5. Mike Mimic

    Jay Tilton Guest

    Mike Mimic <> wrote:

    : do 'text.cf' should simply include code (like C++ include pragma) so
    : that the code of the program would be
    :
    : #!/usr/bin/perl
    : my $test = 0;
    : $test = 1;
    : print "Content-type: text/html\n\n";
    : print $test;

    Actually, it would be like

    #!/usr/bin/perl
    my $test = 0;
    $main::test = 1; # package variable, not lexical
    print "Content-type: text/html\n\n";
    print $test;

    : I have changed the program slightly to show the problem (test.cf is not
    : loaded).
    :
    : test.pl:
    : #!/usr/bin/perl
    : my $test = 0;
    : print "Content-type: text/html\n\n";
    : do 'text.cf';
    ^^^^^^^
    ^

    : print $test;
    :
    : test.cf:
    ^^^^^^^
    ^

    : print "Loaded.\n";
    : $test = 1;
    :
    : It prints only 0.

    It would help if the filenames were the same.
    Including checks on whether the do() succeeds is always a good idea.
    Jay Tilton, Sep 13, 2003
    #5
  6. Mike Mimic

    Mike Mimic Guest

    Hi!

    Jay Tilton wrote:
    > Actually, it would be like
    >
    > #!/usr/bin/perl
    > my $test = 0;
    > $main::test = 1; # package variable, not lexical
    > print "Content-type: text/html\n\n";
    > print $test;


    Changing my $test to our $test does the trick.

    > It would help if the filenames were the same.
    > Including checks on whether the do() succeeds is always a good idea.


    Ups. Thanks. It was only a test script but you are right.

    Thanks, it works now.


    Mike
    Mike Mimic, Sep 13, 2003
    #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. ian douglas
    Replies:
    0
    Views:
    1,805
    ian douglas
    Aug 19, 2003
  2. Ziya Suzen
    Replies:
    0
    Views:
    434
    Ziya Suzen
    Feb 1, 2004
  3. Antonio
    Replies:
    0
    Views:
    642
    Antonio
    May 17, 2004
  4. Bill Warner
    Replies:
    1
    Views:
    454
  5. Replies:
    2
    Views:
    434
    Big and Blue
    Jan 25, 2006
Loading...

Share This Page