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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |