in reply to how to recognize windows and linux os
Why not simply have a list of possible locations the file is allowed to reside, and check them in a well-defined (and documented) order until you hit one? This is a good idea because it won't require any additional logic if you choose to support another OS (with yet another path). Example:
# I assume 'snmpauto' is the name of a file. use File::Spec; my $snmp_file = find_snmpauto(); sub find_snmpauto { my $filename = 'snmpauto'; my @paths = qw(c:\\ /root); for (@paths) { my $name = File::Spec::catfile($_,$filename); return $name if -f $name; } # we hit here only if no file was found. die "Can't find a file '$filename' in the search path (path includ +es:" .join(', ',@paths) .")\n"; }
This will result in the first filename you find to be correct being stored in $snmp_file; it will die (with a list of acceptable paths) if it fails. This way, if you add a new location, you only have to add to the @paths array.
|
|---|