Dear Bretheren, I'm unable to tweak eval so that my super creative piece of code may work on both Linux and Win32 without me commenting out the Win32 lines every time I compile it to work in enemy territory. This code detects COM ports via the Registry and attempts to open/close them to figure out if they are usable or not. On pure Win32 it works, however I want it in my giant perl all-in-one program, which will complaint about certain adversarial modules if asked to be run on Linux. Long story short: the $string in eval statent returns nothing. When run without eval, it works. Please help.

#!/usr/bin/perl use strict; my %RegHash; my $PortObj; my $Registry; my (@keys, @values, $computer, $string); my $os = $^O; my @ok_ports; if ($os =~ /Win/i){ eval { $string = " use Win32; use Win32::TieRegistry ( TiedHash => \'\%RegHa +sh\' ); my \$computer; my \$Registry= \\%RegHash; \@keys = keys( \%{ \$Registry->{\'HKEY_LOCAL_M +ACHINE\\\\HARDWARE\\\\DEVICEMAP\\\\SERIALCOMM\'}}); \@values = values( \%{ \$Registry->{\'HKEY_LOC +AL_MACHINE\\\\HARDWARE\\\\DEVICEMAP\\\\SERIALCOMM\'}}); print \" inside evAl values=\@values keys=\@ke +ys \\n\"; "; print "$string\n"; eval "$string; 1" or warn "inside eval fails! with: $@ +"; eval "use Win32::SerialPort; 1"; my $computer; my $Registry= \%RegHash; print "values =@values keys =@keys \n"; 1 } or die $@; for (my $i=0; $i<@keys; $i++){ my $port_name = $keys[$i]; $port_name =~ s/\\//g; $port_name =~ s/\.//g; next if ($port_name =~ /SmSrl/g); my $port = $values[$i]; print "trying port $port_name which is: $port\n"; eval { if ($PortObj = new Win32::SerialPort($port, 1) +) { #1 is quiet $PortObj->close; push(@ok_ports, $port) } else { print "$port not opening!\n"; } }; sleep (1); } } else { eval "use Device::SerialPort; 1" or die $@; print "I love Linux!\n"; #all ttyS* and ttyUSB* @ok_ports = ('ttyUSB0', 'ttyUSB1'); } print "ports opening are: @ok_ports \n";

In reply to code that runs (and works) on both Linux and Win32 by pashanoid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.