Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^7: dynamic loading modules

by afoken (Monsignor)
on Sep 03, 2012 at 20:10 UTC ( #991511=note: print w/replies, xml ) Need Help??


in reply to Re^6: dynamic loading modules
in thread dynamic loading modules

Perhaps you should have a look at the source of File::Spec. Or perhaps not. Think about the following construct first:

(Each code block represents a properly named file)

package MyProject::OSSpecific::base; use strict; use warnings; sub niceOSName { my $class=shift; die "$class does not implement an OS specific niceOSName() method" +; } sub tempDir { die "I don't know how to find a temp directory"; } sub pathSep { return '/'; } sub installTo { die "Should not happen"; } # much more methods 1;
package MyProject::OSSpecific::doslike; use strict; use warnings; use parent 'MyProject::OSSpecific::base'; sub tempDir { return 'c:\\temp'; } sub pathSep { return '\\'; } # much more methods 1;
package MyProject::OSSpecific::dos; use strict; use warnings; use parent 'MyProject::OSSpecific::doslike'; use Some::DOS::Specific::Module; sub niceOSName { my $x=`command /c ver`; chomp $x; return $x; } sub installTo { return 'C:\myproject'; } 1;
package MyProject::OSSpecific::MSWin32; use strict; use warnings; use parent 'MyProject::OSSpecific::doslike'; use Win32; sub niceOSName { return Win32::GetOSName(); } sub installTo { return 'C:\\Program Files\\MyProject'; } 1;
package MyProject::OSSpecific::unixlike; use strict; use warnings; use parent 'MyProject::OSSpecific::base'; sub niceOSName { my $x=`uname -a`; chomp $x; return $x; } sub tempDir { return '/tmp'; } sub pathSep { return '/'; } # much more methods 1;
package MyProject::OSSpecific::freebsd; use strict; use warnings; use parent 'MyProject::OSSpecific::unixlike'; use Something::Created::Only::For::FreeBSD; sub installTo { return '/usr/local/myproject'; } 1;
package MyProject::OSSpecific::linux; use strict; use warnings; use parent 'MyProject::OSSpecific::unixlike'; use Something::Very::Linux::Specific; sub installTo { return '/opt/myproject'; } 1;
And finally:
packame MyProject::OSSpecific; use strict; use warnings; use parent 'MyProject::OSSpecific::'.$^O; # <-- this is the most impor +tant trick here. 1;
In your code:
use strict; use warnings; use MyProject::OSSpecific; print "This program should be installed to ",MyProject::OSSpecific->in +stallTo(),"\n";

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991511]
help
Chatterbox?
[Eily]: BTW LanX, you should try typing a few random chars at the beginning of each message. This will prevent expansion :P
[LanX]: qwiud you sthink so?
[LanX]: zxwqbd good idea! :)
LanX embraces his new habit spqopiwjdnq
[ambrus]: qQUkZTmHTuKxStGT- BzTIK9gdudif7TkTLI t3mnF144UaAZjkknXY 8nN-QM19wHBsTrp5vB lEYU_Kksa7X1RIBB4x EWLD5X7SW3jGX5ryfN OMn_yL5FTdQxzjhtyX mKN9sjUCzBNHK5Rrp0 S2WMUvIb1i9aZFgjtq VR0GH1bjPMvm1G16iz hBqc1U6toPd4FbJOFj VsOeT745AN1_pO88rD SRAYKtBZwCZedESZmN mvutrOTHiSNwflB- pRfn_k
[Eily]: so far it seems to work
Your Mother reminds the monks they should be grateful not to share an office, lest they be subjugated to constant inanities like, "Czech please!"
[LanX]: what's strange is that the "Cowboy you said this already" message is missing #dqiwd
[LanX]: YM: BTW learn to mute your humanity
[Your Mother]: Cumin? Now I want tacos...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (17)
As of 2017-03-27 16:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.