For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > back to orininal question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author back to orininal question
Mick I Hawkes

2005-04-19, 3:57 am

Guys

Thanks to all the useful suggestions, especially to Charles for the code =
review, I will implement your suggestions throughout my code.

But back to the original question. I need to put a lot of re-occurring =
code into subroutines (as you do) however I am hampered to a problem =
with passing shift I think. This is a very simplified test code where I =
have tried passing shift as an argument and also as a call (see =
commented out bits) both give the same error:

"Error executing run mode 'mode_0': can't call method "param" on an =
undefined value at test_code.pm line 57

here is the code:

package Test_code;
use base 'CGI::Application';
use DBI;
use strict;
use warnings;

########################################
#################################=
#
# Subroutine Name: setup
# Purpose: To set up the start mode and the various run modes...
# It also sets up the database.
# 18-Nov-03 J.Bews :- Initial coding.
# 23-Nov-03 J.Bews :- Changed location for the Templates. Now in the =
same
# location as the HTML files.
# 02-Dec-03 J.Bews :- Converted to OCL_Database.
# 22-Dec-03 J.Bews :- Deleted mode_5. Incorporated into mode_4.
# 14-May-04 J.Bews :- Realised that I was still using the old database
# in the c:\Program Files\Abyss\cgi-bin\OCL_Database subdirectory. =
Changed
# over to the C:\xitami\cgi-bin\OCL_Database subdirectory.
########################################
#################################=
#
sub setup
{
my $self =3D shift;
# Set the start mode.
$self->start_mode('mode_0');
# Set the run modes.
$self->run_modes
(
'mode_0'=3D>'mainmenu',
'mode_1'=3D>'insertproject', =20
);
=20
# Connect to the database using DBI and ODBC.
# Open connection to an Access 97 database called 'Sample_db.mdb'.
my $file =3D "c:\\xitami\\cgi-bin\\ToolBox\\OCL\\OCL.mdb";
my $driver =3D "driver=3D{Microsoft Access Driver (*.mdb)}; =
DBQ=3D$file";
$self->param('mydbh' =3D> DBI->connect("dbi:ODBC:$driver"));
my $dbh =3D $self->param('mydbh');
$dbh->{'LongReadLen'}=3D 5000; # expand the size of the Memo read for =
Access 97
=20
=20
# Path to use for Templates....
#$self->tmpl_path('./'); # ie the same directory as this script.
$self->tmpl_path("c:\\xitami\\webpages\\TestBed\\");
}
########################################
#################################=
#
sub mainmenu
{
my $self =3D shift;
# my @loop =3D GetOfficers($self);
my @loop =3D GetOfficers();
}
########################################
#################################=
#
sub GetOfficers {
#my $self =3D @_;
my $self =3D shift;
my $sql =3D 'SELECT Name as HTML_ProjectName FROM qryOfficer';
my $dbh =3D $self->param('mydbh');
my $sth =3D $dbh->prepare( $sql );
$sth->execute()|| die qq(Could not execute SQL: "$sql");

return $sth->fetchall_arrayref();
}

1;

Can anyone see how I can get the code to see the stuff. (my $dbh =3D =
$self->param('mydbh'); is line 57)

TIA

Mick Hawkes
Jeff 'japhy' Pinyan

2005-04-19, 3:57 am

On Apr 19, Hawkes, Mick I said:

>But back to the original question. I need to put a lot of re-occurring
>code into subroutines (as you do) however I am hampered to a problem with
>passing shift I think. This is a very simplified test code where I have
>tried passing shift as an argument and also as a call (see commented out
>bits) both give the same error:
>
> "Error executing run mode 'mode_0': can't call method "param" on an
>undefined value at test_code.pm line 57


That's because you DIDN'T PASS ANYTHING to the GetOfficers() function.

> sub mainmenu
> {
> my $self = shift;
> # my @loop = GetOfficers($self);
> my @loop = GetOfficers();
> }


You should be doing:

my @loop = GetOfficers($self);

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
Mick I Hawkes

2005-04-19, 3:57 am

Jeff

Like I said in my text. I tried BOTH solutions, it is only commented out =
to show this. Yes I tried that
Format, I commneted out my @loop =3D GetOfficers(); and likewise the =
other bit in main menu.
The crux is my @loop =3D GetOfficers($self); doesn't work either!

=09
On Apr 19, Hawkes, Mick I said:

>But back to the original question. I need to put a lot of re-occurring =


>code into subroutines (as you do) however I am hampered to a problem =

with=20
>passing shift I think. This is a very simplified test code where I =

have=20
>tried passing shift as an argument and also as a call (see commented =

out=20
>bits) both give the same error:
>
> "Error executing run mode 'mode_0': can't call method "param" on an=20
>undefined value at test_code.pm line 57


That's because you DIDN'T PASS ANYTHING to the GetOfficers() function.

> sub mainmenu
> {
> my $self =3D shift;
> # my @loop =3D GetOfficers($self);
> my @loop =3D GetOfficers();
> }


You should be doing:

my @loop =3D GetOfficers($self);

--=20
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
Charles K. Clarkson

2005-04-19, 3:57 am

Hawkes, Mick I <mailto:Michael.I.Hawkes@team.telstra.com> wrote:

: But back to the original question. I need to put a lot of
: re-occurring code into subroutines (as you do) however I am
: hampered to a problem with passing shift I think. This is a
: very simplified test code where I have tried passing shift
: as an argument and also as a call (see commented out bits)
: both give the same error:


One problem I think you are having is defining what 'shift' does.
'shift' is short for 'shift @_'. You are not passing 'shift' around,
you are passing arguments around in @_.

When a subroutine is called, the arguments passed to the sub are
placed in @_ by perl. When an object method is called @_ is prepended
with the calling object. For example, if $self is an object and
method() is a valid method of that object, these two statements are
the same.

$self->method( 'foo' ); # $self is prepended to @_

method( $self, 'foo' ); # The previous call is preferred


Inside the method() subroutine we write this.

sub method {
my $self = shift;
Charles K. Clarkson

2005-04-19, 8:55 am

Hawkes, Mick I <mailto:Michael.I.Hawkes@team.telstra.com> wrote:
: Jeff
:
: Like I said in my text. I tried BOTH solutions, it is only commented
: out to show this. Yes I tried that Format, I commneted out my @loop =
: GetOfficers(); and likewise the other bit in main menu.
: The crux is my @loop = GetOfficers($self); doesn't work either!

Perhaps something else is wrong. Let's take it back to a simpler
case. Here's a minimal module to test this problem. I have eliminated
DBI and calls to HTML::Template. Does the error still occur?

If not, start adding your database stuff back in until the error
happens and you'll know the culprit.

HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328


package Test_code_2;

use base 'CGI::Application';

use strict;
use warnings;
use Data::Dumper 'Dumper';

sub setup {
my $self = shift;
# Set the start mode.

$self->start_mode( 'mode_0' );
# Set the run modes.

$self->run_modes(
mode_0 => 'main_menu',
);

$self->param( mydbh => 'foo' );
}

sub main_menu {
my $self = shift;
my $loop = $self->get_officers();

return $self->query->pre( Dumper $loop );
}

sub get_officers {
my $self = shift;

my $dbh = $self->param( 'mydbh' );

return [ { HTML_ProjectName => 'foo' } ];
}

1;

Mick I Hawkes

2005-04-19, 8:55 am

EUREKA!

It works!

Thanks Charles, it was: my $loop =3D $self->GetOfficers(); that did the =
trick.
(its now beginning to make sense! :-))

Also got the fetchall_arrayref to work too!

Thanks again to everyone!
Cheers
Mick Hawkes
Randal L. Schwartz

2005-04-20, 3:56 pm

>>>>> "Charles" == Charles K Clarkson <cclarkson@htcomp.net> writes:

Charles> One problem I think you are having is defining what 'shift' does.
Charles> 'shift' is short for 'shift @_'.

Or 'shift @ARGV', outside of a subroutine.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com