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

I am working on project by telnet. My project need:
package record;
When I run the program, it told me:
Undefined subroutine &record::get called at ....
I use LWP. What can I do about that? Please help!!! Thanks in advance!!!

Replies are listed 'Best First'.
Re: How to get a package by telnet
by jonadab (Parson) on Apr 28, 2003 at 14:12 UTC

    There are two ways to answer your question. I'm going to start with the obvious way (which takes you at your word and assumes you know what package means).

    Undefined subroutine &record::get called at

    This means you didn't define that subroutine, or maybe you defined it before your package declaration. If you defined sub get first before your package declaration, then it will be main::get rather than record::get. After the package declaration you're in the record package, so get() is the same as record::get(), which is _not_ the same as main::get(). If you need to use get() inside of your package, you should probably declare and define it inside of your package (i.e., after the package declaration). It might be best to put the package declaration at the very beginning of your module, at the top of the file.

    # You start out in package main sub get { print "This is &main::get();\n"; } package record; # This puts you in package record sub get { print "This is &record::get();\n"; }

    The other possibility is that you have misunderstood what package does. If you think "package foo" gives you access to some other foo package that someone else has written so that you can use it, then you have package confused with either use (probably) or require (possibly, if the package is not installed in the usual way). If what you're trying to do is use someone else's package, then you should use it, roughly as follows:

    use record; my $rec = record::get(); my $recfoo = $rec->foo();

    I'm not familiar with the record package, so the above may not be exactly right. See the documentation for the package in question.

    I'm not at all sure that I've answered your question, because I don't understand what telnet has to do with anything, so if these answers don't seem right, maybe you need to clarify the question.


    {my$c;$ x=sub{++$c}}map{$ \.=$_->()}map{my$a=$_->[1]; sub{$a++ }}sort{_($a->[0 ])<=>_( $b->[0])}map{my@x=(& $x( ),$ _) ;\ @x} split //, "rPcr t lhuJnhea eretk.as o";print;sub _{ord(shift)*($=-++$^H)%(42-ord("\r"))};
Re: How to get a package by telnet
by cfreak (Chaplain) on Apr 28, 2003 at 13:53 UTC

    We can't help you unless you post code and what you are trying to do. Are you using Telnet in your program? What are you using LWP for? Are you trying to write a package called 'record' or are you trying to use it? So many questions.

    Making some assumptions: If you're writing a package called record then you need to define a get() subroutine within that package. If you are going to use telnet inside your program then look into Net::Telnet.

    Hope that helps

    Chris

    Lobster Aliens Are attacking the world!
      part of my code like:
      package record; use Class::Struct; struct( section1 => '@', section2 => '@', ); my $summer = new record; my @context1 = qw(sailing swimming); my @context2 = qw(science art); $summer->context1names($i++,$_) for @context1; $summer->context2names($j++,$_) for @context2; print Dumper(\$summer); print "@{$summer->context1names}";
      Please help!!! Thanks!!!
Re: How to get a package by telnet
by arturo (Vicar) on Apr 28, 2003 at 14:29 UTC

    When you put package record; in your script, you are telling perl that, until you say otherwise, everything that follows belongs to that package. So, when you have get($url); in your code, what perl looks for is a subroutine named get in the record package, but it's not finding it. That's what your error message means, anyhow, and what I have to say below is pure speculation, because as others have pointed out, you don't give us enough information.

    I'm guessing your code looks like this:

    use LWP; # or is that LWP::Simple ??? package record; my $url = "http://www.perlmonks.org"; my $content = get( $url );
    (If you don't have the use LWP line at all, then you need to add it, but read on anyway). The problem here is that when you call use LWP;, the get function is imported into the default namespace (aka "main"), but not into the record namespace. So, you could fix the problem by reversing the package and use calls:
    package record; use LWP; my $url = "http://www.perlmonks.org"; my $content = get( $url );
    Which imports the get call from LWP into the record namespace. However, if, as I suspect, you really want to be using the get subroutine from LWP::Simple, you should do this instead:
    package record; use LWP::Simple; my $url = "http://www.perlmonks.org"; my $content = get( $url );
    As others have noted though, typically lowercase packages are used for pragmas and not modules, so you might want to re-think the name record and use Record instead.

    The very gory details. The subroutines' full name is LWP::Simple::get. You can access it through that name anywhere, no matter which package you're in. The use LWP::Simple; directive, along with some behind the scenes stuff, allows you to access that subroutine with a simple get, which perl understands as $CURRENT_PACKAGE::get -- this is called "importing" a symbol (name of something, in this case a subroutine) from one namespace to another. So, when you call use, before package, you import things into the mainpackage, and immediately change the current package. So here's what perl sees, assuming that the first example above is how your code looks:

    # we're in package main by default; any unqualified subroutine referen +ce # is understood to refer to a subroutine in the "main" namespace use LWP::Simple; #main::get is now an alias to LWP::Simple::get package record; my $url = "http://www.perlmonks.org"; my $content = record::get($url); # because that's the package declared + above

    So that's why get($url) doesn't work.

    HTH!

    If not P, what? Q maybe?
    "Sidney Morgenbesser"

Re: How to get a package by telnet
by hardburn (Abbot) on Apr 28, 2003 at 14:00 UTC

    I assume that by "working by telnet", you mean you're logging into a system via telnet and writing code from there. If that is the case, there is nothing special you have to do to the code. Logging in from telnet and writing code is no different from logging onto a local system and writing code.

    I suspect your problem is elsewhere. For one thing, packages with all lowercase names are reserved for pragmas. A quick scan through the Camel didn't show me any existing pragmas named "record" (but perhaps this has changed since it was written?), so the code you have should still work. Even so, you should consider changing it to "My::Record" or something similar. The "My::" namespace is for packages that are never intended to be distributed outside your orginization.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: How to get a package by telnet
by Aristotle (Chancellor) on Apr 28, 2003 at 13:52 UTC
    By the pieces you've shown us, we can only guess. Post some more of the code if you want to have anything resembling a meaningful reply.

    Makeshifts last the longest.