Home > Archive > PERL Beginners > December 2007 > Module help
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]
|
|
| Andy Dixon 2007-12-21, 4:01 am |
| Hello,
I have written a small module with a function that returns some text.
However, when I run it, from a test script, I get:
Undefined subroutine &external::RETURN called at external.pm line 41.
The subroutine is thus:
sub test($) {
my $data = @_;
$data =~ s/cheese/ham/g;
RETURN ($data);
}
I think I may have missed something.. Any help would be wonderful!
Thanks!
Andy
| |
| Wagner, David --- Senior Programmer Analyst --- WG 2007-12-21, 4:01 am |
| > -----Original Message-----
> From: Andy Dixon [mailto:andy@ajd.me.uk]=20
> Sent: Thursday, December 20, 2007 10:37
> To: Perl beginners
> Subject: Module help
>=20
> Hello,
>=20
> I have written a small module with a function that returns some text.
>=20
> However, when I run it, from a test script, I get:
>=20
> Undefined subroutine &external::RETURN called at external.pm line 41.
>=20
> The subroutine is thus:
>=20
> sub test($) {
You are using I believe pro-typing and I believe you are better
off not using.
>=20
> my $data =3D @_;
>=20
> $data =3D~ s/cheese/ham/g;
>=20
> RETURN ($data);
it is return not RETURN
Wags ;)
>=20
> }
>=20
>=20
> I think I may have missed something.. Any help would be wonderful!
>=20
> Thanks!
>=20
> Andy
>=20
> --=20
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>=20
>=20
>=20
****************************************
******************************
This message contains information that is confidential and proprietary to F=
edEx Freight or its affiliates. It is intended only for the recipient name=
d and for the express purpose(s) described therein. Any other use is proh=
ibited.
****************************************
******************************
| |
| Tom Phoenix 2007-12-21, 4:01 am |
| On 12/20/07, Andy Dixon <andy@ajd.me.uk> wrote:
> sub test($) {
Subroutine prototypes, like "($)" here, generally cause more harm than
good, alas. Most programmers should avoid using them in most cases.
> my $data = @_;
You're using the "name" of the array in scalar context; this gives the
number of elements. From the way you're using $data, you probably
wanted to use list context to get the actual element:
my($data) = @_;
> RETURN ($data);
Perl is a case-sensitive language, so the return operator must not be
capitalized. If you've seen RETURN like that in someone else's code,
that's a subroutine call; presumably one that does some special kind
of return operation other than the standard one.
Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
| |
| John W . Krahn 2007-12-21, 4:01 am |
| On Thursday 20 December 2007 10:36, Andy Dixon wrote:
>
> Hello,
Hello,
> I have written a small module with a function that returns some text.
>
> However, when I run it, from a test script, I get:
>
> Undefined subroutine &external::RETURN called at external.pm line 41.
>
> The subroutine is thus:
Do you have warnings and strict enabled?
use warnings;
use strict;
> sub test($) {
Don't use prototypes:
http://library.n0i.net/programming/.../fm_prototypes/
> my $data = @_;
An array in scalar context will return the number of elements of that
array so you need to either use list context:
my ( $data ) = @_;
Or assign a scalar in scalar context:
my $data = $_[ 0 ];
Or:
my $data = shift;
> $data =~ s/cheese/ham/g;
$data will not be modified as it only contains numbers.
You may want to anchor your pattern. Do you want to change the word
'cheesecloth' to 'hamcloth'?
> RETURN ($data);
Perl is case sensitive:
return $data;
> }
John
--
use Perl;
program
fulfillment
| |
| Chas. Owens 2007-12-21, 4:01 am |
| On Dec 20, 2007 2:01 PM, Tom Phoenix <tom@stonehenge.com> wrote:
> On 12/20/07, Andy Dixon <andy@ajd.me.uk> wrote:
>
>
> Subroutine prototypes, like "($)" here, generally cause more harm than
> good, alas. Most programmers should avoid using them in most cases.
snip
You should not use prototypes until you have read and understood
everything in FMTEYEWtKaPiP*, but it basically it boils down to this:
prototypes are meant modify parsing behavior at compile time not
ensure you have the right number or types of arguments. If you want
to control the number and type of arguments (what most people try to
use prototypes for), Perl provides plenty of tools to do so at
runtime:
#!/usr/local/ActivePerl-5.10/bin/perl
use strict;
use warnings;
use feature ':5.10';
use Carp;
my $n;
say "expecting bad number of args:";
eval { $n = n_plus_one(1, 2) };
say $@ ? "got error $@\n" : "\$n is $n\n";
say "expecting bad type";
eval { $n = n_plus_one(2.1) };
say $@ ? "got error $@\n" : "\$n is $n\n";
say "this should work (and return 2):";
eval { $n = n_plus_one(1) };
say $@ ? "got error $@\n" : "\$n is $n\n";
my @a = qw<a b c d e>;
my @b = my @c = @a;
say "expecting bad type:";
eval { $n = are_these_arrays_equal(@a, @b, @c) };
say $@ ? "got error $@\n" : "\$n is $n\n";
say "expecting bad number of args:";
eval { $n = are_these_arrays_equal(\@a) };
say $@ ? "got error $@\n" : "\$n is $n\n";
say "this should work (and return 1)";
eval { $n = are_these_arrays_equal(\@a, \@b, \@c) };
say $@ ? "got error $@\n" : "\$n is $n\n";
sub n_plus_one {
croak "bad number of arguments, expected 1 got " . @_
unless @_ == 1;
my ($n) = @_;
croak "the argument should be an integer, you passed [$n]"
unless $n =~ /^[1-9][0-9]*$/;
return $n + 1;
}
sub are_these_arrays_equal {
croak "bad number of arguments, expected more than one and got " . @_
unless @_ > 1;
croak "all arguments should be array references"
if grep { ref ne 'ARRAY' } @_;
my $one = shift;
while (@_) {
my $two = shift;
return 0 unless @$one ~~ @$two;
$one = $two;
}
return 1;
}
* http://library.n0i.net/programming/.../fm_prototypes/
|
|
|
|
|