http://qs321.pair.com?node_id=532034

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

Hi, The Perl Programming told me use pack; was identical to BEGIN { require pack; import pack; }

so I replaced it, however now i get this error;

DEV> p.pl Unquoted string "import" may clash with future reserved word at p.pl line 7. Not enough arguments for pack at p.pl line 6, near "pack;" syntax error at p.pl line 7, near "import pack" BEGIN not safe after errors--compilation aborted at p.pl line 8.

DEV> perl -v | grep "This is perl"

This is perl, v5.8.0 built for sun4-solaris

Replies are listed 'Best First'.
Re: use x; equivalent
by chromatic (Archbishop) on Feb 22, 2006 at 17:52 UTC

    pack is a Perl keyword, so the parser doesn't know what to do with that construct. I suggest changing the name of your module.

    Of course, if the documentation had shown the unbroken way to call class methods, you wouldn't have had this problem:

    BEGIN { require pack; pack::->import(); }

    Update: fixed the code to use the really unambiguous way.

      Thank you chromatic et al. I changed the name to packe and it worked.

      I was also wondering how to get debug from the importer, I thought this might work, but no. it doesn't print anything.

      BEGIN { require packe; $Exporter::Verbose=1; import packe; }

      Even using your 'unbroken' way, I get

      Not enough arguments for pack at -e line 1, near "pack;" Not enough arguments for pack at -e line 1, near "pack->import" BEGIN not safe after errors--compilation aborted at -e line 1.

      perl v5.6.1

      Good catch, chromatic! Gees, however I missed that!..

      (feeling red faced now)
      _____________________
      "We've all heard that a million monkeys banging on a million typewriters will eventually reproduce
      the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true." - Robert Wilensky, University of California
Re: use x; equivalent
by ikegami (Patriarch) on Feb 22, 2006 at 18:02 UTC

    You're getting problems because pack is a builtin command.

    Also, you're using indirect method call which has problems. Instead of
    BEGIN { require Module; import Module; }
    use
    BEGIN { require Module; Module->import(); }

    Perl provides a dummy import method (in universal base class UNIVERSAL) to save you from checking if the module actually declared an import method.

    So,

    use Module;
    is equivalent to
    BEGIN { require Module; Module->import(); }

    use Module ();
    is equivalent to
    BEGIN { require Module; }

    use Module LIST;
    is equivalent to
    BEGIN { require Module; Module->import(LIST); }

Re: use x; equivalent
by vladb (Vicar) on Feb 22, 2006 at 17:48 UTC
    You must confirm that your pack module implements an import method, as per perldoc
    import There is no builtin "import" function. It is just an ordinary method (subroutine) defined (or inherited) by modules that wish to export names to another module. The "use" function calls the "import" method for the package used. See also the use entry elsewhere in this document, the perlmod manpage, and the Exporter manpage.
    Might think of something else if indeed you had an import method defined in the package you are trying to import.

    Update: Actually, my apologies, you may not need to implement a custom import method afterall. This works for me:

    packg.pm
    package packg; 1;
    BEGIN { require packg; import packg; } print "ok\n";
    And that is with Perl 5.6.1, which could also mean I cannot fully replicate your problem. Maybe include a sample of your package code?
    _____________________
    "We've all heard that a million monkeys banging on a million typewriters will eventually reproduce
    the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true." - Robert Wilensky, University of California