Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

strict and variables

SouldrinkerSouldrinker Member Posts: 143
Hi There,

I'm stuck a little in a think-problem about strict and
perl script-include functions.

Is it possible to have variables in a script (for example
a variable called "$user_id") and give this variables over
to a script I include use the perl-function "do"?

This script would be a possible example for my problem:


file "mother.pl"
[code]

#!/usr/bin/perl

use strict;

my $user_id = 10;

do "child.pl";

print "end of mother script!
";

[/code]

file "child.pl"
[code]

print $user_id;

print "end of child script!
";
[/code]

now my problem appears - the output of the script is the
following:

end of child script!
end of mother script!

but the variable "$user_id" that has got the value "10",
it got in the first line of "mother.pl" is not given out
because it does not have any value in "child.pl" - is there
a way to make this variable "global", to use it in "child.pl"?

would be glad on any help or solution on this problem!

best regards,

sebastian mohrenstecher
executive secretary
net::allies

www.net-allies.de
[email protected]

Comments

  • XfactorXfactor Member Posts: 343
    : Hi There,
    :
    : I'm stuck a little in a think-problem about strict and
    : perl script-include functions.
    :
    : Is it possible to have variables in a script (for example
    : a variable called "$user_id") and give this variables over
    : to a script I include use the perl-function "do"?
    :
    : This script would be a possible example for my problem:
    :
    :
    : file "mother.pl"
    : [code]
    :
    : #!/usr/bin/perl
    :
    : use strict;
    :
    : my $user_id = 10;
    :
    : do "child.pl";
    :
    : print "end of mother script!
    ";
    :
    : [/code]
    :
    : file "child.pl"
    : [code]
    :
    : print $user_id;
    :
    : print "end of child script!
    ";
    : [/code]
    :
    : now my problem appears - the output of the script is the
    : following:
    :
    : end of child script!
    : end of mother script!
    :
    : but the variable "$user_id" that has got the value "10",
    : it got in the first line of "mother.pl" is not given out
    : because it does not have any value in "child.pl" - is there
    : a way to make this variable "global", to use it in "child.pl"?
    :
    : would be glad on any help or solution on this problem!
    :
    : best regards,
    :
    : sebastian mohrenstecher
    : executive secretary
    : net::allies
    :
    : www.net-allies.de
    : [email protected]
    :
    It looks to me like you are wanting to fork another process because you are wanting defined variables passed to the child. If this is what you are intending, then use the fork function.
    If you are wanting to call another script use the system function.

    I hope this helps.
    X
  • aspongeasponge Member Posts: 42
    : Hi There,
    :
    : I'm stuck a little in a think-problem about strict and
    : perl script-include functions.
    :
    : Is it possible to have variables in a script (for example
    : a variable called "$user_id") and give this variables over
    : to a script I include use the perl-function "do"?
    :
    : This script would be a possible example for my problem:
    :
    :
    : file "mother.pl"
    : [code]
    :
    : #!/usr/bin/perl
    :
    : use strict;
    :
    : my $user_id = 10;
    :
    : do "child.pl";
    :
    : print "end of mother script!
    ";
    :
    : [/code]
    :
    : file "child.pl"
    : [code]
    :
    : print $user_id;
    :
    : print "end of child script!
    ";
    : [/code]
    :
    : now my problem appears - the output of the script is the
    : following:
    :
    : end of child script!
    : end of mother script!
    :
    : but the variable "$user_id" that has got the value "10",
    : it got in the first line of "mother.pl" is not given out
    : because it does not have any value in "child.pl" - is there
    : a way to make this variable "global", to use it in "child.pl"?
    :
    : would be glad on any help or solution on this problem!
    :
    : best regards,
    :
    : sebastian mohrenstecher
    : executive secretary
    : net::allies
    :
    : www.net-allies.de
    : [email protected]
    :

    By changing my to our, the scope of $user-id will be extended to include any scripts called by do.

    Kevin
  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    ____ ( http://forcoder.org ) free video tutorials and ebooks about // Objective-C, JavaScript, C, Delphi, Ruby, R, Swift, Visual Basic .NET, PL/SQL, MATLAB, Scratch, C#, Assembly, PHP, Go, Perl, Python, Java, Visual Basic, C++ ML, Lua, Dart, Scheme, ABAP, Bash, Kotlin, Julia, Scala, Apex, Awk, Alice, D, Erlang, VBScript, Ada, Clojure, Transact-SQL, LabVIEW, F#, Crystal, Logo, Rust, Hack, Fortran, SAS, COBOL, FoxPro, Lisp, Prolog // _

Sign In or Register to comment.