Issues when using variables declarad in a package

Hi,

When I try to use variables declared in a package named angel like that :

#------------------------:---------------#
# Module de variables specifiques ANGEL #
#---------------------------------------#

package angel;
#use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK); # à utiliser par défaut
use Exporter qw(import);  # idem
our @ISA=qw(Exporter);  # idem
our @EXPORT=qw(VAR_ANGEL min2maj HEURE HEURE_FIN CONVERT_HEURE);
our @EXPORT_OK=qw(VAR_ANGEL min2maj);

our $FIL_LIV="ListeGlobaleAlivrer_";


When I try to use the variable $FIL_LIV in myScript.pl like this :

#!/usr/bin/perl

### Mise en place de library ###
use lib '/opt/hrdev/bao/dev_bao/INT_QAT_hra/scripts/ANGEL/lib';
use warnings;
use strict;
use File::Basename qw(dirname);
use Cwd qw(abs_path);
use lib dirname(dirname abs_path $0) . '/lib';
use angel;

print "FIL_LIV MAIN :: ",$FIL_LIV,"\n";

I get this error :
Global symbol "$FIL_LIV" requires explicit package name at /opt/hrdev/bao/dev_bao/INT_QAT_hra/scripts/ANGEL/shl/livraison_ENV.pl line 41.

It seems that my perl program doesn't recognize my package. I tried with function in my package but I always have the same negative return.

How can I do to solve this. Can you suggest me another to do that without declaring variables in my main program ?

Thank you for your return.

Seydina
Seydina FallIT ingineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FishMongerCommented:
Why did you comment out the use strict; statement?  You should not do that.

You need to either export the var or specify its fully qualified name.

FQN:
print "FIL_LIV MAIN :: ", $angel::FIL_LIV,"\n";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FishMongerCommented:
I should point out that exporting vars is rarely the proper thing to do.  Instead, the var(s) should be lexically scoped and an accessor module should be written that returns the value.
0
ozoCommented:
package angel;
use vars qw(@ISA @EXPORT @EXPORT_OK); # à utiliser par défaut                    
use Exporter qw(import);  # idem                                                
our @ISA=qw(Exporter);  # idem                                                  
our @EXPORT=qw(VAR_ANGEL min2maj HEURE HEURE_FIN CONVERT_HEURE);
our @EXPORT_OK=qw(VAR_ANGEL min2maj $FIL_LIV);

our $FIL_LIV="ListeGlobaleAlivrer_";

#!/usr/bin/perl
use angel qw($FIL_LIV);                                                        

print "FIL_LIV MAIN :: ",$FIL_LIV,"\n";
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Seydina FallIT ingineerAuthor Commented:
Hi,

I did two things :

1/ Exporting var
    use angel qw($FIL_LIV);

    I've got this error message : "$FIL_LIV" is not exported by the angel module

2/ I create a function in the angel package :

sub VAR_ANGEL {
  my($constantes) =
  {
     $FIL_LIV = "ListeGlobaleAlivrer_",
   };
  return $constantes{shift};
}

In myscript.pl I call the function like this :
$FIL_LIV=VAR_ANGEL($FIL_LIV);

print "FIL_LIV MAIN :: ",$FIL_LIV,"\n";

Result : the var is still empty.

Can you help me with this ?

Papfal
0
ozoCommented:
1/ did angel.pm do
our @EXPORT_OK=qw(VAR_ANGEL min2maj $FIL_LIV);

2/ I'm not sure if you meant
sub VAR_ANGEL {
  my($constantes) =
  {
     $FIL_LIV, "ListeGlobaleAlivrer_",
  };
  return $constantes->{+shift};
}
# in which case $FIL_LIV=VAR_ANGEL($FIL_LIV); would still cause a warning if $FIL_LIV if  $FIL_LIV  is undefined when you call VAR_ANGEL
It also seems unnecessarily Goldbergian when
sub VAR_ANGEL {
  return "ListeGlobaleAlivrer_",
  };
called by
$FIL_LIV=VAR_ANGEL();
would suffice
0
Seydina FallIT ingineerAuthor Commented:
1/ No it didn't, I'm gonna correct it
2/ You're right,my return command in the sub VAR_ANGEL is uncomplete.
I have a lot vars to declare in my sub, ListeGlobaleAlivrer_ is just one of them.
0
Seydina FallIT ingineerAuthor Commented:
1/
Here is the @EXPORT var in angel package :
our @EXPORT=qw(VAR_ANGEL min2maj HEURE HEURE_FIN CONVERT_HEURE $FIL_LIV);

In myscript.pl :
use angel qw($FIL_LIV);
print "FIL_LIV MAIN :: ",$FIL_LIV,"\n";

Result :
FIL_LIV MAIN ::

The var is still empty
0
Seydina FallIT ingineerAuthor Commented:
That solution worked by declaring vars in myscript.pl before calling the function :

sub VAR_ANGEL {
  my($constantes) =
  {
     $FIL_LIV => "ListeGlobaleAlivrer_",
  };
  return $constantes->{+shift};
}

In myscript.pl :
$FIL_LIV=VAR_ANGEL($FIL_LIV);

Now my var seems to be OK.
0
Seydina FallIT ingineerAuthor Commented:
To ozo,

I'm sorry I wanted to share points between you and fishmonger as your both answers helped me to solve my issues. But The way I did it was wrong and I can't go back.

If you know correct this, let me know.

papfal
0
Seydina FallIT ingineerAuthor Commented:
Sorry but I went too fast.
This works with one single var but it don't when I declare several vars in my function localize in the angel package. I'm fed up with this, I don't know what to do...

Here'is the function

sub VAR_ANGEL {
    my($constantes) =
    {
        our $FIL_LIV => "ListeGlobaleAlivrer_",
        our $FIC_LIV_IN => "param_liv_IN_",
        our $FIC_LIV_ERR => "param_liv_ERR_",
    };
    return $constantes->{+shift};
}

In the main

$FIL_LIV=VAR_ANGEL($FIL_LIV);
print "FIL_LIV MAIN :: ",$FIL_LIV,"\n";

Returns me always
FIL_LIV MAIN :: param_liv_ERR_

It always returns the last var of the function.
0
ozoCommented:
I'm not sure what you are trying to do with %{$constantes}
My best guess is that you are trying to do something like

sub VAR_ANGEL {
    return {
       '$FIL_LIV' => "ListeGlobaleAlivrer_",
        '$FIC_LIV_IN' => "param_liv_IN_",
        '$FIC_LIV_ERR' => "param_liv_ERR_",
    }->{+shift};
}


$FIL_LIV=VAR_ANGEL('$FIL_LIV');
0
ozoCommented:
# or could you be trying to do something like
use constant;
0
Seydina FallIT ingineerAuthor Commented:
Your guess is the good one (It is not for nothing guru).
I'm going to take my time and confirm but I think this is the solution to my issue.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.