Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

mention a non-installed module in script

by pashanoid (Scribe)
on Jun 15, 2011 at 05:53 UTC ( [id://909718]=perlquestion: print w/replies, xml ) Need Help??

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

Hello! I've searched and could not find this:

How do I go about "mentioning" a module in my program that does not exist on my system. This particular module WILL NOT install on a Linux system, but will on a Win32 system. However, in the logic of the program this module will not be used unless we're on windows.... example:

my $os = $^O; my $text_os = $c->createText($x0+570,$y0+10,-text=>"ОС: $o +s", -font=>'bar_rus', -fill=>'#afb3b0',-anchor=>'s',-tags => ['os']); eval { if ($os =~ /Win/ig){ use Win32::SerialPort; #we're on Windows } else { use Device::SerialPort; #we're on Linux } }

See, If I run this I get an error like:

[pasha@pelikan tk]$ perl dial.pl Can't locate Win32/SerialPort.pm in @INC (@INC contains: /usr/local/li +b/perl5 /usr/local/share/perl5 /usr/lib/perl5 /usr/share/perl5 /usr/l +ib/perl5 /usr/share/perl5 /usr/local/lib/perl5/site_perl/5.10.0/i386- +linux-thread-multi /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-t +hread-multi /usr/local/lib/perl5/site_perl/5.10.0 /usr/lib/perl5/vend +or_perl/5.10.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl /us +r/lib/perl5/site_perl .) at dial.pl line 33. BEGIN failed--compilation aborted at dial.pl line 33.

But there is no way that I can install the Win32::SerialPort on my Linux box. Nore do I want to :)

Is there a way to have the module mentioned in my program and avoid the interpreter error?

Live long and proseper!

Replies are listed 'Best First'.
Re: mention a non-installed module in script
by ikegami (Patriarch) on Jun 15, 2011 at 06:06 UTC
    BEGIN { if ($os =~ /Win/i){ eval "use Win32::SerialPort; 1" or die $@; } else { eval "use Device::SerialPort; 1" or die $@; } }

    Note: I removed the buggy /g. if (/.../g) not only makes no sense, it can be harmful.

      Thank you, worked like a charm!
Re: mention a non-installed module in script
by ambrus (Abbot) on Jun 15, 2011 at 09:24 UTC
Re: mention a non-installed module in script
by Anonymous Monk on Jun 15, 2011 at 06:49 UTC
Re: mention a non-installed module in script
by 7stud (Deacon) on Jun 15, 2011 at 06:53 UTC

    eval BLOCK catches *run time* exceptions, but a use statement executes at *compile time*, and using a non existent module causes your program to terminate before it can enter runtime. A BEGIN block executes before the rest of the file even compiles.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found