Sandeep Kumar has asked for the wisdom of the Perl Monks concerning the following question:

Hope you all are doing good ! I want to use a variable from script "b.pl script" and "b.pl script" uses a package from "a.pm". In my present "c.pl script" I'm using

require 'b.pl'

so as soon as this line executes, it will run the script "b.pl script". but I need to re-direct the output of "b.pl script" to a text file. Earlier I used to run as

Perl b.pl > textfile.txt

separately but now as I need to access a variable from "b.pl script" I use "require" keyword in "c.pl script" but as it is running the "b.pl script", so I need to do output redirection also. Please suggest me how can I do output rediretion when I'm using require keyword Is it like this ?

require 'b.pl > textfile.txt'

but this seems not working. Please tell me what mistake I'm doing here, is there any way to redirect the output of "b.pl" script without using redirection operator ">". Thanks in advance.

Replies are listed 'Best First'.
Re: Output redirection using “require” in perl
by hippo (Archbishop) on Jul 03, 2018 at 09:23 UTC

    Use select:

    $ cat b.pl #!/usr/bin/env perl use strict; use warnings; print "foo!\n"; $ perl -I. -e 'require "b.pl";' foo! $ perl -I. -e 'open my $o, ">out"; select $o; require "b.pl";' $ cat out foo!

      Adding *STDOUT = $o; to the above, will make it succeed in redirecting also the print statements which explicitly state the filehandle to be STDOUT, e.g. print STDOUT "...":

      perl -I. -e 'open my $o, ">out"; *STDOUT = $o; select $o; require "b.pl";'

      Sandeep Kumar: Note that using select only redirects default prints and writes, it doesn't redirect, for example, print statements explicitly made to a filehandle (print STDOUT ...) and it doesn't redirect the output of external commands.

Re: Output redirection using “require” in perl
by haukex (Archbishop) on Jul 03, 2018 at 09:22 UTC

    I would recommend Capture::Tiny.

    $ cat b.pl use warnings; use strict; print "Hello, I'm b.pl"; $ cat a.pl use warnings; use strict; use Capture::Tiny qw/capture_stdout/; my $stdout = capture_stdout { require 'b.pl'; }; print "<$stdout>\n"; $ perl -I. a.pl <Hello, I'm b.pl>

    Note that using require to execute another Perl file has some caveats: It will search @INC (which as of Perl 5.26 no longer includes the current directory by default), and it will run the file only once, even if you call require multiple times. If that is not desired, I would recommend using do with an absolute pathname instead; make sure to read its documentation on how to check for errors.

Re: Output redirection using “require” in perl
by kcott (Archbishop) on Jul 03, 2018 at 09:47 UTC

    You appear to have posted this as Sandeep Vickey, deleted the title and content, then reposted it as Sandeep Kumar (i.e. the node I'm responding to now).

    The first post is empty as I write this but has been considered for restoration. The link is currently "Reaped:" (the title should change after restoration).

    While it's not good to mess us around like that, I'm more concerned with your multiple accounts. Please read "Site Rules Governing User Accounts" and act accordingly (for however many accounts you may have).

    Please also read "How do I change/delete my post?".

    — Ken

      Yes, it's left a bit of a mess. I would have considered my node for reparenting first if I had seen and considered the nodes in a different order.

      Update: Ok, I've done that now, I guess I'll leave it up to the voters and janitors...

        Yes, that is a mess.

        I'd already voted "Edit" to restore to first one. I've now voted "Edit" to reparent your response; and "Keep" for the OP in this thread.

        — Ken

      Sorry for the mess sir, As I have recently joined the site, so wasn't aware of the rules. I apologize for this. I will surely follow the site rules from now onwards. Thanks & Regards, Sandeep.
Re: Output redirection using “require” in perl
by LanX (Saint) on Jul 03, 2018 at 09:50 UTC
      Actually I lost the credentials for my present user name. So I had to create new one, now as I have recovered the credentials I request to please remove the new username, I would continue with this. Sorry for the fuss and inconvenience caused to all is deeply regretted.
        Actually I lost the credentials for my present user name.

        Did you notice the friendly What's my password? link in the login box? It would have helped you recovering the credentials.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)