in reply to Re: Software Error: Close.pm
in thread Software Error: Close.pm

Hey bobn,

Tried what you suggested by adding the print command but how do I use these lines that you gave me?
$mailer->open(\%headers); print $mailer $body;
When I put these in it says: Global symbol "$body" requires explicit package name

Does this mean I have to declare it first using my?

Sorry cause this is probably a dumb question but still trying to learn. Thanks

Replies are listed 'Best First'.
Re: Re: Re: Software Error: Close.pm
by bobn (Chaplain) on Aug 22, 2003 at 02:39 UTC

    I was just quoting what was in the perldoc.

    Part of the problem is I excerpted too fast leaving out my $mailer = new Mail::Mailer; - see below.

    $mailer->open(\%headers); is giving the open method a reference to the hash %headers, so the following are equivalent:

    my $mailer = new Mail::Mailer; my %headers = ( To => 'test@tester.com, From => 'tester', Subject => 'results of submit', ); $mailer open(\%headers);
    and
    my $mailer = new Mail::Mailer; $mailer->open({ To => 'test@tester.com, From => 'tester', Subject => 'results of submit', });

    Those two are equivalent because the first creates a hash then hands open() a reference to it using the backslash to take the reference. The second creates a hashref in place by enclosing the contents of the hash in curlie brace {} instead of the normal ().

    And yes the error message 'Global symbol "$body" requires explicit package name' does mean you're using strict and need to declare the variable using my. Congratulation on using strict - it's a very good habit to get into.

    Having used the open() method, you would then say:

    print $mailer $query->param('Title'); print $mailer $query->param('Name'); print $mailer $query->param('Position'); # etc for rest of params $mailer->close;

    All code here is untested, BTW.

    --Bob Niederman, http://bob-n.com
      Hey bob thanks for your previous response as it helped HEAPS!! I am now recieving this error which I am not sure why??
      syntax error at enquiry02.pl line 25, near "$mailer open"


      It doesn't like that line were you open the headers. Here is my code so far. I reckon it looks ok but I only a newbie so it properly aint. Can you or anyone see what I did or am doing wrong in a simple terms as you can??? Thanks fellas
      #!/usr/bin/perl -w use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); use Mail::Mailer; my $query = CGI->new(); my @param = qw( Title Name Position School Address Suburb State PCode Email Tel Fax Comments ); my $mailer = new Mail::Mailer; my %headers = ( To => 'test@tester.com', From => 'test', Subject => 'test send stuff', ); $mailer open(\%headers); my $body = ( print $mailer $query->param('Title'), print $mailer $query->param('Name'), print $mailer $query->param('Position'), print $mailer $query->param('School'), print $mailer $query->param('Address'), print $mailer $query->param('Suburb'), print $mailer $query->param('State'), print $mailer $query->param('PCode'), print $mailer $query->param('Email'), print $mailer $query->param('Tel'), print $mailer $query->param('Fax'), print $mailer $query->param('Comments'), ); print $mailer $body; $mailer->close; print $query->redirect('http://www.travancoresch.vic.edu.au/developmen +t/reply02.html');

        syntax error at enquiry02.pl line 25, near "$mailer open"

        This happened because you said:

        $mailer open(\%headers);
        when what was needed was:
        $mailer->open(\%headers);

        open, by itself, is a function defined in the core of perl, used for 'opening' files for reading, writing, etcetera. It wants a file handle, a filename, and possibly a mode, but you gave it a hashref. (And preceded it with $mailer by itself, which is puzzling at best or a syntax error at worst). But $mailer->open() is something else entirely. Because you said my $mailer = new Mail::Mailer;, $mailer is now an object of type Mail::Mailer. The module Mail::Mailer actually defines a routine named open() - same name, seemingly as the core function open() - but if you say $mailer->open(), perl looks at the type of $mailer, sees that it's Mail::Mailer, and uses the open() function defined in that module instead.

        You should check out some doc, like:

        perldoc perlboot
        perldoc perltoot
        
        Look elsewher on this site for tutorials on this subject as well.

        Also, your code still has a problem. Specifically,

        my $body = ( print $mailer $query->param('Title'), print $mailer $query->param('Name'), print $mailer $query->param('Position'), print $mailer $query->param('School'), print $mailer $query->param('Address'), print $mailer $query->param('Suburb'), print $mailer $query->param('State'), print $mailer $query->param('PCode'), print $mailer $query->param('Email'), print $mailer $query->param('Tel'), print $mailer $query->param('Fax'), print $mailer $query->param('Comments'), ); print $mailer $body;
        is wrong. I suggest you go to your command line, issue:
        perldoc Mail::Mailer
        
        and read what comes up,

        (All code given here is UNTESTED unless otherwise stated.)

        --Bob Niederman, http://bob-n.com