Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use without crash?

Status
Not open for further replies.

fykosak

Programmer
Dec 17, 2001
11
CZ
Dear all,
I'm writting a perl script, which should run on both Winz and Linux and might be able to load only platform-dependend modules. If I write something like this:
# supposing $linux indicates OS
if ($linux) {
use Term::ANSIColor;
} else {
#nothing
}
it still doesn't work on Winz, because the compiler searches for the module even when it's not really necessary

any ideas?
robin
 
$linux = "xxx";
if($linux) this is always true !!!
if($linus eq "xxx") is true !
if($linux eq "yyy") is false. ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
I don't think you can conditionally use() modules (not sure, though). You can, however do it with require since require's are done at runtime rather than at compile time like use's.
Code:
if ($linux) {
   require Term::ANSIColor;
   ## might need to import functions here if they are not exported already
   # import LIST
}

jaa
 
jamisar:

for any of
Code:
 $linux = '';
  $linux = 0;
  $linux = undef;
'[tt]if ($linux)[/tt]' will be false.

jaa
 
i agree. ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
but he said: # supposing $linux indicates OS
..... you understand me jaa ?? ja! ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
Ah, good point. I guess it depends if $linux is a flag variable (1 or 0) or contains the OS as a string.

jaa
 
nene: it depends if $linux is loaded or not.
to clear the situation, he should
1) choise a clear var-name, not $linux but $os_type
2) load this var: $os_type = "what-ever-you-want";
3) check it: if ($os_type eq linux) do ...; else(if ...) do ... ; else do ... ; and so on
--------------
PS: i really miss the 'C'-switch-statement in perl :(
this one of the reasons: i don't like perl.
'perl' (writted by a 'c'guru) is for people lazy to learn a really computer-language (like 'c' is), and they don't realize: the syntax is to 90% the SAME.
all what 'perl' can, does 'c' faster!
sure you need more knowhow, but it's good for you, you really get a chance to learn a really native computer-language. (i say: not interpreted)
every times you download perl, you get problems.
last time was: $xx="ok";
now you have to say: (local|my|what-ever-you-want) $xx = "ok";
----------
i hate the construct: if...else if...else
this is the worst case debugging code.

------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
What?! No switch construct!? You must be crazy, this is Perl ... We can do (almost) anything.
Perl's (admittedly (somewhat) klunky) switch statement emulation:
Code:
use constant default => 1;
for ($os_type) {

    /Linux/     and     do { &cool_stuff;           last; };
    /MacOSX/    and     do { &different_cool_stuff; last; };
    /Windows/   and     do { die;                   last; };
    default     and     do { &last_resort;          last; };
}
Stick whatever you like inside the do-block as you would a C switch, just end it with a [tt]last[/tt] (like break).

Bah! No switch statement my derriere! ;-)

jaa
 
please admit it jaa,
you perfectly know, the pseudo perl-switch
never will reach the funcionallity of his c-colleague.
as exemple:
a = 3;
switch(a){
case 3: func_3();
case 2: func_2();
case 1: func_1();
default: break;
}
will call all 3 funcs if a == 3
calls func_2 + func_1 if a == 2
calls func_1 if a == 1
calls nothing if a != 1,2,3

sure 'if' can also do the job, i wrote "don't like if-else statements" :) ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
Hmmmm

I think that 'use' is evaluated at compile time - so that line will *always* be executed no matter what the value of $linux is.

I think you need to use 'require' rather than 'use'.
Mike
________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
I just came across this method in another piece of code. Seems like a simple solution now, but eval() is to me as pack()/unpack() once were: an obscure, shadowy function whose use in all but the simplest of form fills me with trepidation. It's definitely not the first tool that I pull out of my toolbox. Enough about my insecurities, though, here's the code:
Code:
if ($linux) {
    eval 'use Term::ANSIColor';
}

jaa
 
Will that really work? I'll test it later. I'm curious since I'm covering the chapter in the Adv.Perl (panther) on eval.

--jim
 
I did some testing and it seems to work as well as require. If there are INIT blocks in the module they don't get executed, but they don't when using require either. BEGIN blocks do get executed, however, and Exported functions and variables still get exported. Not sure what other problems could arise.

jaa
 
I recall that eval 'use ....' fails with NT Mike
________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top