Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

use x; equivalent

by unclesmiff (Initiate)
on Feb 22, 2006 at 17:41 UTC ( #532034=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on use x; equivalent
Select or Download Code
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
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.

      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

      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

      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; }
Re: use x; equivalent
by ikegami (Pope) 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); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://532034]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-12-28 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls