duckyd has asked for the wisdom of the Perl Monks concerning the following question:

I sometimes see folks using this syntax for calling subroutines:
use Module::Foo; Module::Foo::do_something();
rather than
use Module::Foo qw/do_something/; do_something();
I prefer the latter in almost all cases. I think it avoids a lot of mostly unneccesary clutter.

If the method is only used once in the module or script, then I can see a justification for the former syntax, and it does make it very clear where the function is coming from, but for the most part, I don't think it's worth the extra clutter.

I am curious what other folks think. Which syntax do you prefer, and why? I thought I'd seen something about this in perldoc perlstyle, but I can't find it - is there a community consensus?

Replies are listed 'Best First'.
Re: prefered syntax for using imported subs
by jeffa (Bishop) on Mar 09, 2006 at 02:11 UTC

    Well, i really prefer the OO version:

    use Module::Foo; my $foo = Module::Foo->new(); $foo->do_something();
    but to answer your question, i too prefer the latter. Take CGI.pm for example -- i enjoy using that module the most when i use its function-oriented interface:
    print header, start_html( -title => 'Decimal to Hex Converter', -bgcolor => 'black', -text => 'white', ), h1({align=>'center'},'Decimal to Hex Converter'), ;
    But it can be easy to mix up your subroutines with the subroutines from the module(s) you are importing, and programs can really benefit from the OO approach in ways other than preventing name collision:
    my $cgi = CGI->new; my $tmpl = HTML::Template->new( filehandle => \*DATA, associate => $cgi, ); print $cgi->header, $tmpl->output; __DATA__ <html> yadda ...
    In that example, "magic" happens when you instantiate the CGI object and pass the reference to the HTML::Template constructor. You get forms that remember their values when the user submits invalid form information.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: prefered syntax for using imported subs
by adrianh (Chancellor) on Mar 09, 2006 at 10:28 UTC
    If the method is only used once in the module or script, then I can see a justification for the former syntax, and it does make it very clear where the function is coming from, but for the most part, I don't think it's worth the extra clutter.

    I sometimes use it when I'm writing OO code so that I don't import subroutines into the current package to avoid them being inherited by subclasses.