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

Howdy monks,

magazines usually require listings to not exceed a certain column width. But what to do if your listing pulls in a module like

###<--- max column width --->### use Foo::Bar::Baz::Really::Long::Module::Name;
and you would like it to fit into the column as indicated above? Is there a way to split the line in a syntactically correct format without further obfuscation like variable substitution?

Replies are listed 'Best First'.
Re: How to split long "use Foo::Bar" lines?
by PodMaster (Abbot) on May 24, 2004 at 15:45 UTC
    Yes, no , not really, kind of.
    `perldoc -f use'
    `perldoc -f require'
    use File'Spec'Win32; # shorten it like so BEGIN { require join '/', qw[ Foo Bar Baz Really Long Module Name.pm ]; }
    You could turn that into a module used like
    use m 'Foo::Bar::Baz' . 'Really::Long::Module::Name';
    but that seems just dumb to me. Of course, cpan never disappoints (:
    Module::Load
    Module::Require

    update: Here's a better idea, have the magazine use line numbers
    1: use strict;
    2: ###<--- max column width --->###
    3: use Foo::Bar::Baz::Really::Long+
    ::Module::Name;
    4: use CGI;
    ...

    update: thanks ysth, yes, I mean't Name.pm (typo fixed).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      I think you mean Name.pm, not Name in that qw[].

      Adapting the 'use m' idea, from 5.6.2 onward, you could say:

      ###<--- max column width --->### use if 1, "Foo::Bar::Baz::". "Really::Long::". "Module::Name";

      Isn't the Ada-like package seperator deprecated? Even if not, I still think it's ugly. It tends to confuse syntax-highlighting editors, too.

      ----
      send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Re: How to split long "use Foo::Bar" lines?
by EvdB (Deacon) on May 24, 2004 at 15:51 UTC
    It is fairly common to see this presented as:
    ###<--- max column width --->### use Foo::Bar::Baz::Really::\ Long::Module::Name;
    There is also usually a note saying that lines ending in \ should be unsplit. This is consistent with bash notation so some people wll be familiar with it.

    --tidiness is the memory loss of environmental mnemonics

      As a a I tried:
      #!/usr/bin/perl -w use Data::\ Dumper; print (@ARGV);
      But when I run that I get this:
      Can't locate Data/.pm in @INC (@INC contains: /usr/lib/perl5/5.8.2/cyg +win-thread-multi-64int /usr/lib/perl5/5.8.2 /usr/lib/perl5/site_perl/ +5.8.2/cygwin-thread-multi-64int /usr/lib/perl5/site_perl/5.8.2 /usr/l +ib/perl5/site_perl .) at ./test.pl line 4. BEGIN failed--compilation aborted at ./test.pl line 4.
        It is not supposed to work in perl -> it is just a markup for the code that the user reads. It is the same as doing something like this:
        # Fill in user details. $user->set('name', 'joe'); $user->set('tel', '01234567'); ... $user->set('email', 'joe@test.com');
        The '...' will not compile - it is just there to represent more stuff. The '\' at the end of a line means 'don't wrap here'.

        --tidiness is the memory loss of environmental mnemonics

Re: How to split long "use Foo::Bar" lines?
by revdiablo (Prior) on May 24, 2004 at 17:03 UTC

    Perhaps something like the following will work for you:

    eval "use Some::Really" . "::Long::Package" . "::Name";
Re: How to split long "use Foo::Bar" lines?
by Plankton (Vicar) on May 24, 2004 at 15:48 UTC
    I don't think there is anyway to get around using a variable. I wonder if Perl 6 addresses this. I am reminded of Ada. I think in Ada this could of been handled like so ...
    ---<--- max column width --->### with Foo.Bar.Baz.Really; use Foo.Bar.Baz.Really; with Long.Module.Name; use Long.Module.Name; procedure call_proc_defined_package_Name is begin a_proc_defined_package_Name("Hello World!"); end call_proc_defined_package_Name;

    Plankton: 1% Evil, 99% Hot Gas.