Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

accessing values

by frank1 (Beadle)
on Oct 14, 2020 at 20:57 UTC ( [id://11122842]=perlquestion: print w/replies, xml ) Need Help??

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

How can i print the values from different filename using require

perl file 1.pl #!/usr/bin/perl my $var1 = "hello"; my $var2 = " perl!";
perl 2.pl #!/usr/bin/perl require '1.pl'; print $var1; print $var2;

Replies are listed 'Best First'.
Re: accessing values
by NetWallah (Canon) on Oct 15, 2020 at 03:35 UTC
    The functionality you seek is traditionally provided by a "config file".

    Here is a sample, using Config::Tiny:

    $ cat myconfig.ini var1=hello var2=perl
    $ perl -MConfig::Tiny -e 'my $Config = Config::Tiny->read("myconfig.i +ni"); print $Config->{_}{var1},"\n", $Config->{_}{var2},"\n"' hello perl

                    "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

      Thank you. this solution has worked for me

Re: accessing values
by choroba (Cardinal) on Oct 14, 2020 at 21:12 UTC
    Variables declared with my in 1.pl are defined at file scope: once the file ends, they're out of scope. Neither require nor do can see them.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      so there is no way i can access variables outside the file

        > so there is no way i can access variables outside the file

        sure, but they need to be package variables declared with our

        The cleanest way is to define an own package "File1".

        Then you can either export them or reference them via $File1::var1

        If you rally want both files to be in the same package ("main" is default), then you won't need to fully qualify them by a leading package name.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re: accessing values
by Fletch (Bishop) on Oct 14, 2020 at 21:20 UTC

    As has been mentioned lexicals at a file scope aren't accessible outside that file. What you've written is (more or less) equivalent to trying to do this:

    { my $var1 = "hello"; my $var2 = "perl!"; } print $var1; print $var2;

    If you tried to do that under strict you'd get errors immediately because you're trying to access variables after they've gone out of scope. See Coping with Scoping.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-19 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found