searching global variables

Discussion in 'Perl Misc' started by Kiss Gabor, Dec 9, 2008.

  1. Kiss Gabor

    Kiss Gabor Guest

    I have need a code snippet those input is a text of a perl subroutine
    (e.g. coming from a file) and the output of list of global variables
    used by the sub.
    So far I created something like this:

    -------------8<-------------------8<------------------
    # $code holds the source of subroutine. E.g.:
    $code = q{ sub { my $aaa = $bbb + $ccc; return $aaa; } };

    my %vars;
    open(PARSER,q{perl -c -e 'use strict; }.$code.q{' 2>&1 |}) or die;
    while (<PARSER>) {
    next unless /Global symbol "\$(\w+)" requires explicit package name at/;
    $vars{$1}++;
    }
    close PARSER;

    my $varlist;
    foreach (keys %vars) {
    $varlist .= qq{our \$$_; };
    }

    # In the second pass globals do not cause error.
    # Let's see if syntax is correct otherwise.
    open(PARSER,q{perl -c -e 'use strict; }.$varlist.$code.q{' 2>&1 |}) or die;
    while (<PARSER>) {
    # check other errors or '-e syntax OK' message
    }
    close PARSER;

    # Global variables are keys of %vars (I.e. 'bbb' and 'ccc')
    -------------8<-------------------------8<------------------

    However this is not an elegant solution. I wonder if there is any way
    to omit one or both fork+exec.

    Any hints will be appreciated.

    Gabor
     
    Kiss Gabor, Dec 9, 2008
    #1
    1. Advertising

  2. Kiss Gabor

    Guest

    On Tue, 9 Dec 2008 15:46:47 +0000 (UTC), (Kiss Gabor) wrote:

    >I have need a code snippet those input is a text of a perl subroutine
    >(e.g. coming from a file) and the output of list of global variables
    >used by the sub.
    >So far I created something like this:
    >
    >-------------8<-------------------8<------------------
    ># $code holds the source of subroutine. E.g.:
    >$code = q{ sub { my $aaa = $bbb + $ccc; return $aaa; } };
    >
    >my %vars;
    >open(PARSER,q{perl -c -e 'use strict; }.$code.q{' 2>&1 |}) or die;
    >while (<PARSER>) {
    > next unless /Global symbol "\$(\w+)" requires explicit package name at/;
    > $vars{$1}++;
    >}
    >close PARSER;
    >
    >my $varlist;
    >foreach (keys %vars) {
    > $varlist .= qq{our \$$_; };
    >}
    >
    ># In the second pass globals do not cause error.
    ># Let's see if syntax is correct otherwise.
    >open(PARSER,q{perl -c -e 'use strict; }.$varlist.$code.q{' 2>&1 |}) or die;
    >while (<PARSER>) {
    > # check other errors or '-e syntax OK' message
    >}
    >close PARSER;
    >
    ># Global variables are keys of %vars (I.e. 'bbb' and 'ccc')
    >-------------8<-------------------------8<------------------
    >
    >However this is not an elegant solution. I wonder if there is any way
    >to omit one or both fork+exec.
    >
    >Any hints will be appreciated.
    >
    >Gabor


    If the object of an open is an expression, then sure why not?

    sln

    ===================

    sub { my $aaa = $bbb + $ccc; return $aaa; }

    open(PARSER,perl -c -e 'use strict; sub { my $aaa = $bbb + $ccc; return $aaa; }
    ' 2>&1 |) or die;

    our $VARS;

    open(PARSER,perl -c -e 'use strict; VARLIST sub { my $aaa = $bbb + $ccc; return
    $aaa; } ' 2>&1 |) or die;
     
    , Dec 9, 2008
    #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. Wayne
    Replies:
    2
    Views:
    475
    Wayne
    Nov 11, 2003
  2. jubelbrus
    Replies:
    5
    Views:
    619
    JohnQ
    Jul 20, 2007
  3. mark4asp
    Replies:
    1
    Views:
    221
  4. Tony Archer

    Global Variables? Multi-Application Variables?

    Tony Archer, Nov 24, 2003, in forum: ASP General
    Replies:
    5
    Views:
    225
    Tony Archer
    Nov 25, 2003
  5. stumblng.tumblr
    Replies:
    1
    Views:
    208
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page