Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Illegal character in prototype for main

Reply
Thread Tools

Illegal character in prototype for main

 
 
hillgoogle@charter.net
Guest
Posts: n/a
 
      08-23-2011
I have an issue in my perl and its due to my mis-underatanding of perl
sub.

In my main pgm i have this statement that is calling my mail program.
It works fine UNLESS i have a user like:
tim.o'(E-Mail Removed). When my user is just like
http://www.velocityreviews.com/forums/(E-Mail Removed) then its fine. The error is "Illegal
character in prototype for main::SendMail : $,$,$ "

&SendMail($eml,$mail_text,$mail_sub);

sub SendMail($,$,$)
{
#who the email is being sent to
my $To = shift;

#the message body
my $mymsg = shift;

#the mail subject
my $mysubject = shift;

#From email address
my $from = "me\@somecompany.com";

my $msg = new MIME::Lite
To =>$To,
From =>$from,
Subject =>$mysubject,
Type =>'text/html',
Data =>$mymsg;

$msg->send;
}

Should I be putting these 3 vars in an array and then using that
array?

Thanks for your help. Mike
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      08-23-2011
>>>>> "h" == hillgoogle <(E-Mail Removed)> writes:


h> In my main pgm i have this statement that is calling my mail program.
h> It works fine UNLESS i have a user like:
h> tim.o'(E-Mail Removed). When my user is just like
h> (E-Mail Removed) then its fine. The error is "Illegal
h> character in prototype for main::SendMail : $,$,$ "

h> &SendMail($eml,$mail_text,$mail_sub);

don't use & for calling subs. actually it will also bypass the prototype
you are using

h> sub SendMail($,$,$)

and speaking of prototypes, they are not a good thing in general and
rarely needed. the , is not a prototype char and that is the bug you are
seeing. it should just be $$$. but since you bypass it by calling with &
you can see it is of little use here. just drop using them unless you
find a reason to use them.
h> {
h> #who the email is being sent to

useless comment.

h> my $To = shift;

h> #the message body
h> my $mymsg = shift;

h> #the mail subject
h> my $mysubject = shift;

much better to just assign to a list of vars:

my( $to, $msg, $subject ) = @_ ;

h> #From email address
h> my $from = "me\@somecompany.com";

use single quotes for literals without interpolation. and then you don't
need the \@

my $from = '(E-Mail Removed)';


h> my $msg = new MIME::Lite

don't use indirect object calls. perldoc perlobj explains why. use a
direct call like this:

MIME::Lite->new(

uri

--
Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
------------ Perl Developer Recruiting and Placement Services -------------
----- Perl Code Review, Architecture, Development, Training, Support -------
 
Reply With Quote
 
 
 
 
hillgoogle@charter.net
Guest
Posts: n/a
 
      08-23-2011
On Aug 23, 4:21*pm, "Uri Guttman" <(E-Mail Removed)> wrote:
> >>>>> "h" == hillgoogle *<(E-Mail Removed)> writes:

>
> * h> In my main pgm i have this statement that is calling my mail program.
> * h> It works fine UNLESS i have a user like:
> * h> tim.o'(E-Mail Removed). When my user is just like
> * h> (E-Mail Removed) then its fine. The error is "Illegal
> * h> character in prototype for main::SendMail : $,$,$ "
>
> * h> &SendMail($eml,$mail_text,$mail_sub);
>
> don't use & for calling subs. actually it will also bypass the prototype
> you are using
>
> * h> sub SendMail($,$,$)
>
> and speaking of prototypes, they are not a good thing in general and
> rarely needed. the , is not a prototype char and that is the bug you are
> seeing. it should just be $$$. but since you bypass it by calling with &
> you can see it is of little use here. just drop using them unless you
> find a reason to use them.
> * h> {
> * h> #who the email is being sent to
>
> useless comment.
>
> * h> my $To = shift;
>
> * h> #the message body
> * h> my $mymsg = shift;
>
> * h> #the mail subject
> * h> my $mysubject = shift;
>
> much better to just assign to a list of vars:
>
> * * * * my( $to, $msg, $subject ) = @_ ;
>
> * h> #From email address
> * h> my $from = "me\@somecompany.com";
>
> use single quotes for literals without interpolation. and then you don't
> need the \@
>
> * * * * my $from = '(E-Mail Removed)';
>
> * h> my $msg = new MIME::Lite
>
> don't use indirect object calls. perldoc perlobj explains why. use a
> direct call like this:
>
> * * * * MIME::Lite->new(
>
> uri
>
> --
> Uri Guttman *-- *uri AT perlhunter DOT com *--- *http://www.perlhunter.com--
> ------------ *Perl Developer Recruiting and Placement Services *-------------
> ----- *Perl Code Review, Architecture, Development, Training, Support -------


Uri, so you are saying my code shold be as follows:
************************************************** ****************************

SendMail($eml,$mail_text,$mail_sub);

sub SendMail($$$)
{
my ($To, $mymsg, $mysubject) = @_;

my $from = "me\@somecompany.com";

my $msg = MIME::Lite->new(
To =>$To,
From =>$from,
Subject =>$mysubject,
Type =>'text/html',
Data =>$mymsg);

$msg->send;
}

************************************************** ****************************
Right?
Mike
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-23-2011
"Uri Guttman" <(E-Mail Removed)> writes:
>>>>>> "h" == hillgoogle <(E-Mail Removed)> writes:


[...]

> h> &SendMail($eml,$mail_text,$mail_sub);
>
> don't use & for calling subs.


At least not when calling them with arguments. &something is useful in
subroutines performing some 'auxiliary task' and then 'forwarding' the
call to another routine: It means 'invoke something and use the
current @_ as argument list'. Semantically, this is (mostly)
equivalent to something(@_) except that it is shorter and the overhead
of creating a new @_ whose content is identical to the present @_ is
avoided.
 
Reply With Quote
 
hillgoogle@charter.net
Guest
Posts: n/a
 
      08-23-2011
On Aug 23, 4:41*pm, Rainer Weikusat <(E-Mail Removed)> wrote:
> "Uri Guttman" <(E-Mail Removed)> writes:
> >>>>>> "h" == hillgoogle *<(E-Mail Removed)> writes:

>
> [...]
>
> > * h> &SendMail($eml,$mail_text,$mail_sub);

>
> > don't use & for calling subs.

>
> At least not when calling them with arguments. &something is useful in
> subroutines performing some 'auxiliary task' and then 'forwarding' the
> call to another routine: It means 'invoke something and use the
> current @_ as argument list'. Semantically, this is (mostly)
> equivalent to something(@_) except that it is shorter and the overhead
> of creating a new @_ whose content is identical to the present @_ is
> avoided.


OK ... then it should be:

************************************************** ****************
&SendMail($eml,$mail_text,$mail_sub);

sub SendMail($$$)
{
my ($To, $mymsg, $mysubject) = @_;

my $from = '(E-Mail Removed)';

my $msg = MIME::Lite->new(
To =>$To,
From =>$from,
Subject =>$mysubject,
Type =>'text/html',
Data =>$mymsg);

$msg->send;
}
************************************************** ****************

Better? TY Mike
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-23-2011
>>>>> "h" == hillgoogle <(E-Mail Removed)> writes:

h> Uri, so you are saying my code shold be as follows:

h> SendMail($eml,$mail_text,$mail_sub);

h> sub SendMail($$$)

drop the prototype.

sub SendMail {

and most put the opening { on the sub line.

h> {
h> my ($To, $mymsg, $mysubject) = @_;

don't mix upper/lower case names. also why the my prefix? they are
scoped with my so $msg and $subject are better names.

also indent code in the sub body.

h> my $from = "me\@somecompany.com";

you didn't change the quotes as i showed you.

h> my $msg = MIME::Lite->new(

h> To =>$To,
h> From =>$from,
h> Subject =>$mysubject,
h> Type =>'text/html',
h> Data =>$mymsg);

some white space helps there. also don't put the close ); on the data
line. it is hard to see and means you can't cut/paste that line or move
the close ) as easily.

h> $msg->send;
h> }

h> Right?

not completely but better. this is how i would do it. note ALL the
little changes like whitespace and name fixes.

send_mail( $eml, $mail_text, $mail_sub );

sub send_mail {

my( $to, $msg, $subject ) = @_;

my $from = '(E-Mail Removed)';

my $msg = MIME::Lite->new(
To => $to,
From => $from,
Subject => $subject,
Type => 'text/html',
Data => $msg
);

$msg->send() ;
}

uri

--
Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
------------ Perl Developer Recruiting and Placement Services -------------
----- Perl Code Review, Architecture, Development, Training, Support -------
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-23-2011
>>>>> "h" == hillgoogle <(E-Mail Removed)> writes:

h> On Aug 23, 4:41*pm, Rainer Weikusat <(E-Mail Removed)> wrote:
>> "Uri Guttman" <(E-Mail Removed)> writes:
>> >>>>>> "h" == hillgoogle *<(E-Mail Removed)> writes:

>>
>> [...]
>>
>> > * h> &SendMail($eml,$mail_text,$mail_sub);

>>
>> > don't use & for calling subs.

>>
>> At least not when calling them with arguments. &something is useful in
>> subroutines performing some 'auxiliary task' and then 'forwarding' the
>> call to another routine: It means 'invoke something and use the
>> current @_ as argument list'. Semantically, this is (mostly)
>> equivalent to something(@_) except that it is shorter and the overhead
>> of creating a new @_ whose content is identical to the present @_ is
>> avoided.


h> OK ... then it should be:

h> ************************************************** ****************
h> &SendMail($eml,$mail_text,$mail_sub);

no.

h> sub SendMail($$$)

and no again.

drop both the & and the prototype. one disables the other and the
prototype is useless to you here. it is not a good feature in general
and has only a few very narrow uses.

uri

--
Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
------------ Perl Developer Recruiting and Placement Services -------------
----- Perl Code Review, Architecture, Development, Training, Support -------
 
Reply With Quote
 
Wolf Behrenhoff
Guest
Posts: n/a
 
      08-23-2011
Am 23.08.2011 23:46, schrieb (E-Mail Removed):
> On Aug 23, 4:41 pm, Rainer Weikusat <(E-Mail Removed)> wrote:
>> "Uri Guttman" <(E-Mail Removed)> writes:
>>>>>>>> "h" == hillgoogle <(E-Mail Removed)> writes:

>>
>> [...]
>>
>>> h> &SendMail($eml,$mail_text,$mail_sub);

>>
>>> don't use & for calling subs.

>>
>> At least not when calling them with arguments. &something is useful in
>> subroutines performing some 'auxiliary task' and then 'forwarding' the
>> call to another routine: It means 'invoke something and use the
>> current @_ as argument list'. Semantically, this is (mostly)
>> equivalent to something(@_) except that it is shorter and the overhead
>> of creating a new @_ whose content is identical to the present @_ is
>> avoided.

>
> OK ... then it should be:
>
> ************************************************** ****************
> &SendMail($eml,$mail_text,$mail_sub);
>
> sub SendMail($$$)


No! Re-read what has been written. Using the & overrides possible
prototypes - and in addition (and that's what Rainer's comment was
about) it passes all current arguments to the sub if you don't use
parameters.

Maybe an example can explain this better:

sub foo {
print "Foo: @_";
}
sub bar { &foo }
bar("Hello", 42);

Here you're calling bar with two parameters. bar then calls foo. Since &
has a special meaning, the program will output Foo: Hello 42. Try
removing the & here!

To summarise: don't use & if you don't know what it is for.


Please use this:

SendMail($eml,$mail_text,$mail_sub);

sub SendMail {
...
}
 
Reply With Quote
 
Charlton Wilbur
Guest
Posts: n/a
 
      08-24-2011
>>>>> "u" == Uri Guttman <(E-Mail Removed)> writes:

u> drop the prototype.

u> sub SendMail {

u> and most put the opening { on the sub line.

Don't make me get my torch and pitchfork.

Charlton


--
Charlton Wilbur
(E-Mail Removed)
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      08-24-2011
On Tue, 23 Aug 2011 17:55:19 -0400 "Uri Guttman" <(E-Mail Removed)> wrote:

UG> not completely but better. this is how i would do it. note ALL the
UG> little changes like whitespace and name fixes.

send_mail( $eml, $mail_text, $mail_sub );

sub send_mail {

my( $to, $msg, $subject ) = @_;

my $from = '(E-Mail Removed)';

my $msg = MIME::Lite->new(
To => $to,
From => $from,
Subject => $subject,
Type => 'text/html',
Data => $msg
);

$msg->send() ;
}

(end Uri's example)

I wanted to suggest something perhaps more generally useful, where all
the parameters you pass to your function will simply become hash key
overrides. Also note the warning at the end.

Ted

#+begin_src perl

#!/usr/bin/perl

use warnings;
use strict;
use Data:umper;
use MIME::Lite;

send_mail( From => '(E-Mail Removed)', To => "you", Data => "message data", Subject => "subject you want" );

sub send_mail {

my %settings = (
To => "Default Recipient",
From => "Default Sender",
Subject => "Default Subject",
Type => 'text/html',
Data => "Default Message Text"
);

my %overrides = @_;

$settings{$_} = $overrides{$_} foreach sort keys %overrides;

my $msg = MIME::Lite->new(%settings);
$msg->send() if defined $msg;
warn "Could not use settings to create message " . Dumper(\%settings)
unless defined $msg;
}

#+end_src
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prototype WTP 0.2 released,this release for Prototype 1.6.0 javascript fish Javascript 0 10-11-2008 07:35 AM
Class prototype vs C function prototype June Lee C++ 2 04-13-2008 08:17 PM
Prototype Object.extend(new Base() | Hash | Hash.prototype) usage: jacobstr@gmail.com Javascript 3 03-27-2007 07:56 AM
relation between prototype and Prototype.js shypen42@yahoo.fr Javascript 9 05-26-2006 01:13 AM



Advertisments