Gathering Source files

Discussion in 'Perl Misc' started by Ray Muforosky, Apr 21, 2005.

  1. Help.

    If I have file1 that loads (require file2 and files), is it possible
    save the content of file file after the load.

    example:
    file1
    #!perl
    $a =12;
    require file2;
    $b = $z;
    require file3;
    $c = $y;
    print $a;


    file2
    $z = 200;
    1;

    file3
    $y = "foo";
    1;

    So, is it possible to save file1 after loading file2 and file3
    file1 becomes:
    #!perl
    $a =12;
    $z = 200;
    $b = $z;
    $y = "foo";
    $c = $y;
    print $a;


    Thanks
    Ray
     
    Ray Muforosky, Apr 21, 2005
    #1
    1. Advertising

  2. Ray Muforosky

    Guest

    "Ray Muforosky" <> wrote:
    > Help.
    >
    > If I have file1 that loads (require file2 and files), is it possible
    > save the content of file file after the load.
    >
    > example:
    > file1
    > #!perl
    > $a =12;
    > require file2;
    > $b = $z;
    > require file3;
    > $c = $y;
    > print $a;
    >
    > file2
    > $z = 200;
    > 1;
    >
    > file3
    > $y = "foo";
    > 1;
    >
    > So, is it possible to save file1 after loading file2 and file3
    > file1 becomes:
    > #!perl
    > $a =12;
    > $z = 200;
    > $b = $z;
    > $y = "foo";
    > $c = $y;
    > print $a;


    Those are not (in general) the same thing, because of the file scoping
    of lexical variables and such. I know that in the example you give there
    are no lexical variables, but then again in the example you give it is
    trivially easy to do what you want by hand. So I have to assume that the
    real world extends beyond your example.

    A not very generic (or good) solution would be something like this:

    warn "Untested";
    $program1 =~ s/\brequire ([^;]+);/"\n".get_file($1)."\n"/eg;

    sub get_file {
    ## see perldoc -f require for how to find file when not in cwd.
    open my $fh, "<", "$_[0].pm" or die "$_[0]: $!";
    local $/;
    return <$fh>;
    };

    Among other things that would break this would be quoted things that have
    the word "require" within them, so look at Filter::Simple and such modules
    to fix that.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Apr 21, 2005
    #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. Farhang  Farid

    Gathering CVS statistics with Java

    Farhang Farid, Jan 23, 2005, in forum: Java
    Replies:
    1
    Views:
    465
    Loek Raemakers
    Jan 23, 2005
  2. =?Utf-8?B?QmVuIFIu?=

    Idea gathering, suggestions welcome

    =?Utf-8?B?QmVuIFIu?=, Apr 17, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    685
    Steven Cheng[MSFT]
    Apr 19, 2006
  3. Mike
    Replies:
    41
    Views:
    1,197
    Hilarion
    Nov 21, 2004
  4. Phlip
    Replies:
    1
    Views:
    535
  5. Xavier
    Replies:
    3
    Views:
    315
    Sean Ross
    Jul 7, 2003
Loading...

Share This Page