Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

accessing values

by frank1 (Novice)
on Oct 14, 2020 at 20:57 UTC ( #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 #!/usr/bin/perl my $var1 = "hello"; my $var2 = " perl!";
perl #!/usr/bin/perl require ''; 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 (Archbishop) on Oct 14, 2020 at 21:12 UTC
    Variables declared with my in 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 (Chancellor) 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?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2020-10-31 10:23 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (287 votes). Check out past polls.