Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: How can I (safely) use packages of the same name but different versions?

by xevian (Sexton)
on Mar 12, 2008 at 18:12 UTC ( [id://673790]=note: print w/replies, xml ) Need Help??


in reply to How can I (safely) use packages of the same name but different versions?

For those who were interested in the problem, I was able to have the "new" code run in a "safe" way by forking... forking copies the symbol table and changes to it in the child process are not mirrored to the parent process.
Here's a quick snippet showing the method...
use Data::Dumper; sub foo { if ( open(FROM, "-|" ) ) { # PARENT local($/); my $output = <FROM>; # from the child my $retval = eval "$output"; my @values = @$retval; return $values[0] if( @values == 1 ); return @values; } else { # CHILD unshift( @INC, "/path/to/NEW"); findAndRequire( 'My::Package' ); my @output = My::Package::func(); # local not really needed here, but who is counting local $Data::Dumper::Purity = 1; local $Data::Dumper::Terse = 1; print Dumper( \@output ); # to the parent exit; } } sub findAndRequire { my ($pkgName) = @_; $pkgName =~ s/::/\//g; # search @INC for a file which matches foreach my $dir (@INC) { my $file = "${dir}/${pkgName}.pm"; if( -e $file ) { require "$file"; last; } } } # Call our "proxied" function! my $output = foo(); local $Data::Dumper::Purity = 1; local $Data::Dumper::Terse = 1; print "OUTPUT = " . Dumper( $output );
  • Comment on Re: How can I (safely) use packages of the same name but different versions?
  • Download Code

Replies are listed 'Best First'.
Re^2: How can I (safely) use packages of the same name but different versions?
by samtregar (Abbot) on Mar 12, 2008 at 18:57 UTC
    That should work fine as long as all your code is fork-safe. Beware though, DBI connections will cause you all manner of problems when forking, even if the children don't use DBI at all.

    But before you go, would it kill you to tell us why you want to do this in the first place? Inquiring minds want to know!

    -sam

      Thanks for the word of warning about the DBI stuff, I'll be sure to spend some extra time testing that when this actually gets integrated :)

      The reason for doing this is because we have a product which supports adding entities that are essentially sandboxed codebases. The catch here is that these entities can be different versions of the product. (I am not a fan of this, but it's water under the bridge at this point).

      Each codebase has at least one well-known method across all versions, and there are methods in the base product that will iterate over all entities and call this method on each one (to initialize it).

      The snags happen when the codebases are different versions, not just from eachother but also from the main product (which by happenstance also has a definition of this method).

      So, in effect, this solution is necessary to compensate for the requirement that these entities support different versions running on the same machine.
        Water under the bridge huh? Be careful, I think that water has a good chance of turning into a flash flood and washing your whole bridge away!

        -sam

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-19 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found