Ramprasad A Padmanabhan wrote:
Is there anyway I can do a using namespace for a particular loop like in c++;
( for functions not exported )

Below are a few dirty little tricks that can get you something similar, but I've never used them, and I'm not sure they are good practice...


Use at your own risk!

#!/usr/bin/perl

use strict;
use warnings;

use File::Spec ();

for my $o ( 'File::Spec' ) {
    print 'tmpdir: ' . $o->tmpdir(), "\n";
    print 'curdir: ' . $o->rel2abs( $o->curdir() ), "\n";
}

print '#' x 60, "\n";

do {
    print 'tmpdir: ' . $_->tmpdir(), "\n";
    print 'curdir: ' . $_->rel2abs( $_->curdir() ), "\n";
} for 'File::Spec';

print '#' x 60, "\n";

{
    local *tmpdir = sub { File::Spec->tmpdir(@_) };
    print 'tmpdir: ' . tmpdir(), "\n";
}

print '#' x 60, "\n";

use import 'File::Spec' => qw( tmpdir rel2abs curdir );

print 'tmpdir: ' . tmpdir(), "\n";
print 'curdir: ' . rel2abs( curdir() ), "\n";

__END__

And here is the 'import.pm' module used in the final example above:

package import;

sub import {
    my $caller = caller();
    my $package = shift;
    my $module  = shift;
    foreach my $meth ( @_ ) {
        *{"${caller}::${meth}"} = sub { $module->$meth( @_ ) };
    }
}

1;

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to