Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Open a .pl file form a .pl file

by Anonymous Monk
on Sep 18, 2002 at 15:43 UTC ( [id://198871]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can I run this
open(XX,"<test_two.pl"); my @test=<XX>; close(XX); my $xxx=join("",@test); print $xxx;
in my test_one.pl and print the results. When I run the program the results on the browser is the code itself from test_two and not the execution of it. Is it possible and does anyone know how it can be done? Thnaks!

Replies are listed 'Best First'.
Re: Open a .pl file form a .pl file
by kabel (Chaplain) on Sep 18, 2002 at 15:51 UTC
    you could do these:

    using a pipe:
    open (XX, "perl -w test_two.pl |"); my @test = <XX>; close (XX);
    using backquotes:
    my @test = `perl -w test_two.pl`;
Re: Open a .pl file form a .pl file
by Abstraction (Friar) on Sep 18, 2002 at 15:50 UTC
    my $output = `perl test_two.pl`; print $output;
Re: Open a .pl file form a .pl file
by Molt (Chaplain) on Sep 18, 2002 at 15:52 UTC

    Read through perldoc -f eval. The following shows a quick demo.

    #!/usr/bin/perl use strict; use warnings; open FILE, "test1.pl" or die "Cannot read test1.pl: $!\n"; my $file = join '', <FILE>; close FILE; eval $file; # Check for errors. die $@ if $@;
      Wouldn't it be much simpler to do something along the lines of;

      do "test.pl";

      tlhf

Re: Open a .pl file form a .pl file
by Dog and Pony (Priest) on Sep 18, 2002 at 16:15 UTC
    You may want to look up 'do' (perldoc -f do).

    That is, if 'test_two.pl' prints something and you just want to forward that print, you can execute your program with do for that.

    If program number two is returning something, you can capture the result from do and print that.

    Example code:

    ## test_one.pl: #!/usr/bin/perl use warnings; use strict; # $bar will hold "Bar", # "Foo" gets printed right away. my $bar = do 'test_two.pl'; print $bar; ## test_two.pl: print "Foo\n"; return "Bar\n";

    This is a somewhat faulty solution in case you want to mangle the output from the second script, but if you don't, this is the simplest way, without executing external programs or reading files and evaling.


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
Re: Open a .pl file form a .pl file
by BrowserUk (Patriarch) on Sep 18, 2002 at 16:16 UTC

    I think that you would be better off looking at the documentation for require rather than using eval. If your first program looks like this.

    #! perl -sw use strict; require 'test2.pl'; print weather();

    And your second program like this (test2.pl)

    #! perl -sw use strict; sub weather { # do stuff to gen data return 'This is the generated data'; } 1;

    When test1.pl is run it gives the following output

    C:\test>test This is the generated data C:\test>

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
Re: Open a .pl file form a .pl file (use do)
by Flexx (Pilgrim) on Sep 18, 2002 at 16:17 UTC

    If you want to execute an external perl script, you can use the

    do EXPR

    command (check your perldoc -f do).

    This will evaluate EXPR and take it's result as the filename to execute.

    You should not use qx`` (backticks), unless you need to pass arguments, or need control over the scripts output. It's more expensive than just do-ing the script. Apart from the pipeing overhead, calling the other script using backticks would start another perl instance to run it -- which will demand extra memory, of course.

    Same applies to opening a pipe to the external script, the way kabel sugested. Update: You only want to use pipes if you need to input to the other script's STDIN.

    So long,
    Flexx

Re: Open a .pl file form a .pl file
by krujos (Curate) on Sep 18, 2002 at 15:57 UTC
    You are not executing the code; all you are doing is opening it and reading the file. To call the code and get the results, as you want you need to use back ticks.
    $test = `test_two.pl`;
    Read the CGI tutorials to get yourself off to a good start.
    Good luck
    Josh
    UPDATE: fixed newlines...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://198871]
Approved by fireartist
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found