Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Scoping problem!!

by cool (Scribe)
on May 10, 2006 at 12:06 UTC ( #548433=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks!

1)Let say I am calling another Perl script (B.pl) from A.pl. What is the best way of doin so? I am using "do B.pl" at present.

2)What abt the global variable declaration. Will 'our' for a variable in A.pl work for B.pl as well? What abt 'require' and 'use'.

I am using 5.8.5.

Thank you for your valuable time

Replies are listed 'Best First'.
Re: Scoping problem!!
by blazar (Canon) on May 10, 2006 at 12:09 UTC

    perldoc -f do, perldoc -f require, perldoc -f use. If you ask me, you are better off making B.pl a real module, but then if it's really called like that, then don't call it B.pm, since the latter already exists and is in core.

    All in all it's hard to suggest you a good strategy without knowing what A.pl and B.pl really do. For sure what you're asking is awkward, as such, and most people will smell a XY Problem here.

      ... and to add to this excellent advice, take a look at Including files in order to help you solve X
Re: Scoping problem!!
by swampyankee (Parson) on May 10, 2006 at 21:24 UTC

    In regards to question 2:

    "Scoping" applies only within a single program; A.pl and B.pl are independent programs, so they have no variables in common. The only ways (of which I'm aware) to get information from one to the other are by environment variables (if, for example, B.pl is forked from A.pl), command line arguments, or some form of interprocess communication, such as pipes.

    As pointed out by the prior respondents, you've got a couple of choices, depending on your application design: re-write B.pl as a module (with a nice, descriptive name that does not clobber a core module), rewrite A.pl to include the required functionality of B.pl, or re-write both to use some form of interprocess communication. This could be as simple as something like

    A.pl ]arguments for A.pl] | B.pl [arguments for B.pl]


    …but probably not.


    update: fixed markup

    emc

    "Being forced to write comments actually improves code, because it is easier to fix a crock than to explain it. "
    —G. Steele
Re: Scoping problem!!
by ptum (Priest) on May 10, 2006 at 16:44 UTC

    I almost never have two Perl scripts calling each other. In most cases, either:

    • (a) they are integrated closely enough that one (or both) should really be a module, and I end up instantiating one from the other (or a controller script) and calling its methods, or
    • (b) they are independent enough that I have them both talk to a database or a file and trigger them independently by some external event.

    Just my $0.02 worth ...


    No good deed goes unpunished. -- (attributed to) Oscar Wilde

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2020-02-22 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (98 votes). Check out past polls.

    Notices?