Professional Web Applications Themes

Passing a variable to a package function vs. a local function - PERL Beginners

I'm a bit new to this so please bear with me... I've written a script that uses CGI.pm something like this: use CGI::Carp qw(fatalsToBrowser); use CGI qw(:all); $query = new CGI; .....blah...blah... &myfunc($query); .....blah...blah... sub myfunc{ my ($query) = _; $foo=$query->param("foo"); ...more...blah...blah... } Everything works fine as is, but I'm trying to take the function "myfunc" out and put it in a separate .pm file because I need to call it from several cgi scripts. When I put it in the .pm file, I get something like: Can't locate object method "param" via package "Trend" at /usr/local/lib/perl5/site_perl/5.005/MMC/Dex.pm line 253, <INFILE> ...

  1. #1

    Default Passing a variable to a package function vs. a local function

    I'm a bit new to this so please bear with me...

    I've written a script that uses CGI.pm something like this:

    use CGI::Carp qw(fatalsToBrowser);
    use CGI qw(:all);

    $query = new CGI;

    .....blah...blah...
    &myfunc($query);
    .....blah...blah...

    sub myfunc{ my ($query) = _;
    $foo=$query->param("foo");
    ...more...blah...blah...
    }

    Everything works fine as is, but I'm trying to take the function "myfunc"
    out and put it in a separate .pm file because I need to call it from several
    cgi scripts. When I put it in the .pm file, I get something like:

    Can't locate object method "param" via package "Trend" at
    /usr/local/lib/perl5/site_perl/5.005/MMC/Dex.pm line 253, <INFILE> chunk
    1150.

    I do have:
    use CGI::Carp qw(fatalsToBrowser);
    use CGI qw(:all);
    In the .pm file

    What am I missing?

    Thanks,
    -Dan

    Dan Fish Guest

  2. #2

    Default Re: Passing a variable to a package function vs. a local function

    Dan Fish wrote:
    > I'm a bit new to this so please bear with me...
    >
    > I've written a script that uses CGI.pm something like this:
    >
    > use CGI::Carp qw(fatalsToBrowser);
    > use CGI qw(:all);
    >
    > $query = new CGI;
    >
    > ....blah...blah...
    > &myfunc($query);
    > ....blah...blah...
    >
    > sub myfunc{ my ($query) = _;
    > $foo=$query->param("foo");
    > ...more...blah...blah...
    > }
    >
    > Everything works fine as is, but I'm trying to take the function "myfunc"
    > out and put it in a separate .pm file because I need to call it from several
    > cgi scripts. When I put it in the .pm file, I get something like:
    >
    > Can't locate object method "param" via package "Trend" at
    > /usr/local/lib/perl5/site_perl/5.005/MMC/Dex.pm line 253, <INFILE> chunk
    > 1150.
    >
    > I do have:
    > use CGI::Carp qw(fatalsToBrowser);
    > use CGI qw(:all);
    > In the .pm file
    >
    > What am I missing?
    >
    > Thanks,
    > -Dan
    >
    If you have name the package file as say Mypackage.pm
    then in your mail program you will call the function as
    Mypackage::myfunc($query).

    Ram



    Ramprasad A Padmanabhan Guest

  3. #3

    Default Re: Passing a variable to a package function vs. a local function

    On Sun, Sep 28, 2003 at 11:48:19PM -0700, Dan Fish wrote:
    > sub myfunc{ my ($query) = _;
    > $foo=$query->param("foo");
    > ...more...blah...blah...
    > }
    >
    > Everything works fine as is, but I'm trying to take the function "myfunc"
    > out and put it in a separate .pm file because I need to call it from several
    > cgi scripts. When I put it in the .pm file, I get something like:
    >
    > Can't locate object method "param" via package "Trend" at
    > /usr/local/lib/perl5/site_perl/5.005/MMC/Dex.pm line 253, <INFILE> chunk
    > 1150.
    Either use the pure subroutine call instead of the method call.

    Trend::myfunc( $query );

    Or use the method call, and know that the first argument will
    be the class/object on which the method was invoked.

    package Trend;
    sub myfunc {
    my ($package, $query) = _;
    ...
    }

    package main;
    Trend->myfunc( $query );

    --
    Steve
    Steve Grazzini Guest

Similar Threads

  1. Passing a ROWTYPE to a function
    By Eric Brown in forum PostgreSQL / PGSQL
    Replies: 2
    Last Post: January 5th, 04:50 PM
  2. Passing db values to function
    By Darby in forum ASP Database
    Replies: 2
    Last Post: August 8th, 10:33 PM
  3. passing a function as a ref?
    By James O'Reilly in forum Macromedia Flash Actionscript
    Replies: 8
    Last Post: February 20th, 03:41 PM
  4. Passing a hash to a function
    By Mark Healey in forum PERL Miscellaneous
    Replies: 2
    Last Post: August 25th, 05:41 AM
  5. Passing 'this' to a function
    By Gary Monk in forum ASP.NET General
    Replies: 3
    Last Post: July 23rd, 10:59 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139