use Win32::TieRegistry if ($^O =~ /win32/i)

S

shelden0

I thought this would be really easy...how do I conditionally use a
package? I.e., only use the Registry Package if I'm on Windows?

I tried wrapping in evals, etc., but they don't seem to work. Perhaps
because compliation errors cannot be trapped?

Thanks,
Brian Shelden
(e-mail address removed)
 
M

Mothra

I thought this would be really easy...how do I conditionally use a
package? I.e., only use the Registry Package if I'm on Windows?

I tried wrapping in evals, etc., but they don't seem to work. Perhaps
because compliation errors cannot be trapped?

Thanks,
Brian Shelden
(e-mail address removed)

Take a look at this
http://tinyurl.com/rg28p

Hope this helps

Mothra
 
P

Paul Lalli

I thought this would be really easy...how do I conditionally use a
package? I.e., only use the Registry Package if I'm on Windows?

Assuming you have a recent version of perl (I think 5.8.0 or higher,
maybe?), see:
perldoc if

If you don't have that recent a version of perl, either download this
module from CPAN (http://search.cpan.org/~ilyaz/if-0.0401/if.pm) or use
an eval on the two separate components of a 'use' statement. (See
perldoc -f use)
I tried wrapping in evals, etc., but they don't seem to work.

"does not work" is the worst of all possible error descriptions. *how*
does it not work? Syntax error? Runtime error? Incorrect output?
Computer blows up?

In fact, in this case, it must also be asked *what* does not work?
Where is the code that demonstrates what you tried? How can we help
you fix your code if you don't show us your code?
Perhaps because compliation errors cannot be trapped?

Ahh, there's the old standby. "I can't figure out how to do this,
therefore it must not be possible." Do you even realize how supremely
arrogant that question is?

Please read the posting guidelines for this group. They will help you
to ask better questions and therefore to get better answers.

Paul Lalli
 
S

shelden0

I thought this would be really easy...how do I
Assuming you have a recent version of perl (I think 5.8.0 or higher,
maybe?), see:
perldoc if

Ah. Thanks. But I can't guarantee the users of my script will
have a specific version of perl.

Although I'm using 5.8.8 on win32 and 5.8.[67] on Linux/Solaris, I
don't think I can guarantee the level of my users. Some of whom
would rather not be using perl in the first place.
If you don't have that recent a version of perl, either download this
module from CPAN (http://search.cpan.org/~ilyaz/if-0.0401/if.pm) or [...]

....similar issue. It would be better if I needn't force my users to
use a
module if it's not included everywhere.

Especially since I am sure there is a way to get this to work
without resorting to external code. (And there is, see below.)
or use an eval on the two separate components of a 'use'
statement. (See perldoc -f use)

Huh? What are the two separate components of a use?
use Module VERSION LIST? Require Module; import Module?
"does not work" is the worst of all possible error descriptions. *how*
does it not work? Syntax error? Runtime error? Incorrect output?
Computer blows up?

In fact, in this case, it must also be asked *what* does not work?
Where is the code that demonstrates what you tried? How can we help
you fix your code if you don't show us your code?

By "does not work" I meant "I was unable to include conditionally
a package." (I think that's the first paragraph of my post.)

But yes, the error message would have been helpful. Sorry about that.

To wit:
Can't locate win32/TieRegistry.pm in @INC (@INC contains: ...

I think we can agree that the Unix @INC contents are irrelevant?

And the code is in the subject line, mate.
Ahh, there's the old standby. "I can't figure out how to do this,
therefore it must not be possible." Do you even realize how supremely
arrogant that question is?

Not as arrogant as your reply.

Maybe I should have said 'cannot be trapped *by eval*.'

I'm sorry if I came across as arrogant. My intention was to show
that I'd RTFM'd. I thought I saw in perldoc -f eval that that eval()
doesn't trap compilation errors? But now I don't see the reference.

In any event:
eval {
use win32::TieRegstry;
}

Does not give me the chance to check if that eval worked, it just
crashes the script (with the @INC message.)

I'm 100% sure that it's something that I'm doing wrong, mate!
Please read the posting guidelines for this group. They will help you
to ask better questions and therefore to get better answers.

Point taken.

Thank you, Mertha, for a working example of what I'm trying to do.

For reference:
BEGIN {
if ($^O =~ /win32/i) {
require win32::TieRegistry;
}
else {
print "not on win32. So we'll parse files, not registry.\n";
}
}

Brian Shelden
(e-mail address removed)
 
A

Alan Stewart

Assuming you have a recent version of perl (I think 5.8.0 or higher,
maybe?), see:
perldoc if

Ah. Thanks. But I can't guarantee the users of my script will
have a specific version of perl.

Although I'm using 5.8.8 on win32 and 5.8.[67] on Linux/Solaris, I
don't think I can guarantee the level of my users. Some of whom
would rather not be using perl in the first place.
If you don't have that recent a version of perl, either download this
module from CPAN (http://search.cpan.org/~ilyaz/if-0.0401/if.pm) or [...]

...similar issue. It would be better if I needn't force my users to
use a
module if it's not included everywhere.

Especially since I am sure there is a way to get this to work
without resorting to external code. (And there is, see below.)
or use an eval on the two separate components of a 'use'
statement. (See perldoc -f use)

Huh? What are the two separate components of a use?
use Module VERSION LIST? Require Module; import Module?
"does not work" is the worst of all possible error descriptions. *how*
does it not work? Syntax error? Runtime error? Incorrect output?
Computer blows up?

In fact, in this case, it must also be asked *what* does not work?
Where is the code that demonstrates what you tried? How can we help
you fix your code if you don't show us your code?

By "does not work" I meant "I was unable to include conditionally
a package." (I think that's the first paragraph of my post.)

But yes, the error message would have been helpful. Sorry about that.

To wit:
Can't locate win32/TieRegistry.pm in @INC (@INC contains: ...

I think we can agree that the Unix @INC contents are irrelevant?

Is this the error when the OS is Win32 or when the OS is NOT Win32 ??
And the code is in the subject line, mate.


Not as arrogant as your reply.

Maybe I should have said 'cannot be trapped *by eval*.'

I'm sorry if I came across as arrogant. My intention was to show
that I'd RTFM'd. I thought I saw in perldoc -f eval that that eval()
doesn't trap compilation errors? But now I don't see the reference.

In any event:
eval {
use win32::TieRegstry;
}

Does not give me the chance to check if that eval worked, it just
crashes the script (with the @INC message.)

I'm 100% sure that it's something that I'm doing wrong, mate!


Point taken.

Thank you, Mertha, for a working example of what I'm trying to do.

For reference:
BEGIN {
if ($^O =~ /win32/i) {
require win32::TieRegistry;
}
else {
print "not on win32. So we'll parse files, not registry.\n";
}
}

The module is Win32::TieRegistry, not win32::TieRegistry. The
reference code shown will fail just as you said under Win32.

Did you cut&paste or re-(mis)-type it here?

Alan Stewart
 
P

Paul Lalli

Especially since I am sure there is a way to get this to work
without resorting to external code. (And there is, see below.)

I never claimed there wasn't. You did.
Huh? What are the two separate components of a use?
use Module VERSION LIST? Require Module; import Module?

Yes. If you read the doc I pointed you to, you see that
use Module;
is the same as
BEGIN {
require Module;
import Module;
}

In your case, you do not want the BEGIN{} portion, because you want to
do some error checking. So you want to wrap the two components in an
eval.
But yes, the error message would have been helpful. Sorry about that.

No worries.
To wit:
Can't locate win32/TieRegistry.pm in @INC (@INC contains: ...

I think we can agree that the Unix @INC contents are irrelevant?
Yes.

And the code is in the subject line, mate.

Please don't do that. Put all the relevant details of the post in the
post itself. Do not expect people to bounce around from post to
subject to figure everything out for you. Again, read the posting
guidelines.
Not as arrogant as your reply.

No no. My reply was condescending (as is this statement), not
arrogant.
Maybe I should have said 'cannot be trapped *by eval*.'

No, because that's still false. What you should have said was "because
I haven't figured out how to do it yet."
I'm sorry if I came across as arrogant. My intention was to show
that I'd RTFM'd. I thought I saw in perldoc -f eval that that eval()
doesn't trap compilation errors? But now I don't see the reference.

In any event:
eval {
use win32::TieRegstry;
}

Does not give me the chance to check if that eval worked, it just
crashes the script (with the @INC message.)

Right, because as the docs for use() show you, use happens at compile
time. Perl has not tried to run the eval yet, because it saw and
immediately executed the use statement. This is why you have to take
the two components of use out of the implicit BEGIN{} block and list
them explicitly in your eval.
I'm 100% sure that it's something that I'm doing wrong, mate!

Good. Next time, please do not imply otherwise.
Thank you, Mertha, for a working example of what I'm trying to do.

For reference:
BEGIN {
if ($^O =~ /win32/i) {
require win32::TieRegistry;

Please note that by omitting the second component of use, you may be
destroying the functionality of the module. I have no idea whether or
not Win32::TieRegistry exports anything or not. If it does, you have
failed to import whatever was exported.
}
else {
print "not on win32. So we'll parse files, not registry.\n";
}
}

The general case that I was alluding to of using the eval would be:

eval {
require Win32::TieRegistry;
import Win32::TieRegistry;
} ;
warn "Unable to load Win32::TieRegistry\n" if $@;

Paul Lalli
 
P

Paul Lalli

Alan said:
The module is Win32::TieRegistry, not win32::TieRegistry. The
reference code shown will fail just as you said under Win32.

No it won't.
Did you cut&paste or re-(mis)-type it here?

Did you run it to see what happens?

The dangers of working with a broken operating system.
require "win32::TieRegistry";
is the same as
require "win32/TieRegistry.pm"

which, as you may or may not know, Windows considers to be exactly the
same path as "Win32/TieRegistry.pm"

Such case-sensitive mistakes only come about when importing modules,
not just requiring them. At that point, Perl is unable to find any
such package named win32::TieRegistry, and fails to import the code
from the package Win32::TieRegistry, because it was never asked for.

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top