develooper Front page | perl.beginners | Postings from September 2009

Re: text html from file to a scalar and mail

Thread Previous | Thread Next
From:
Shlomi Fish
Date:
September 12, 2009 07:16
Subject:
Re: text html from file to a scalar and mail
Message ID:
200909121715.42077.shlomif@iglu.org.il
Hi John!

Nice to meet you, and welcome to Perl. I'll try to answer as well as I can. 
Note that I will also give you some general Perl best practices and links that 
may not be a cause of the problems you are having.

On Saturday 12 September 2009 11:32:20 John Plum wrote:
> HI Folk,
> 
> May I introduce myself, John Plumridge, London, UK.
>       - I'm still in awe of this whole creation we're in.
> 
> Nice to meet you.
> 

:-)

> 
> I have a reason of course, for approaching you, via my MTNews 'console'.
> What a great application!
> 
> MY problem is obtaining and passing text/html from file to a scalar
> variable to be printed to mail, (using MIME::Creator).
> 
> Unfortunately I get: 'printFile(FileHandle=GLOB(0x84b66b8))'
> 

This means it is a typeglob (a generic entry in the Perl 5 packages (= 
namespaces)) that is blessed into the FileHandle class. Generally, 
"FileHandle" is deprecated and you should use IO::Handle and friends instead.

>  The reference assigned to the scalar variable is from a FileHandle sub
> routine. However, with a similar reference to sub routines, i.e.
> OrderFromForm_html(),  I get the output printed to mail without a
> problem. That sub routine  is perl processed form data passed from a
> preceding visible page in browser - 'form.html')
> 
> With mixed success then, I'v worked hard at this. Take a look:
> 
> ###----signature (html)from external file----###
> 
> my $signature_file = "/path_to/signature.html";
> 

It's nice that you're declaring your variables using "my", but why don't you 
have the "use strict;" and "use warnings;" pragmata? See:

* http://www.perlmonks.org/?node_id=111088

* http://perl-begin.org/tutorials/perl-for-newbies/part2/#page--my--DIR

In case, they were included, you should have placed them here, so we won't be 
led into believing they were omitted from the original program.

> use FileHandle;
>    my $signature = new FileHandle;
>       $signature->open("<$signature_file")or die "Could not open
> file\n";

You should use IO::Handle instead of File::Handle (or IO::File in your case), 
and use the three args open. It's nice you've used die.

$signature is not a good name for a filehandle - better call it $signature_fh.

> 
> 
> sub printFile($) {

Don't use prototypes.

>  my $fileHandle = $_[0];

Accessing $_[$idx] is not very robust - you should do:

<<<
my $fileHandle = shift;
>>>

Or:

<<<
my ($fileHandle) = @_;
>>>

>     while (<$fileHandle>) {
>         my $line = $_;

Just do: 

<<<
while (my $line = <$fileHandle>) {
>>>

>         chomp($line);
>         print "$line\n";
> $fileHandle->close(); # automatically closes file

You shouldn't close the fileHandle inside the line reading loop, as you won't 
be able to read from it further.

>     }
> }
> 
> 
> 
> #---Assemble/concatenate references in both ascii and html, to make full
> confirmatory message bodies with order details---
> 
> $scalar_sig = "\printFile($signature)";

Perl won't call printFile for you from within the string. Neither will the 
"print" be returned to the string. 

Please do:

<<<
my $scalar_sig = slurpFile($signature);
>>>

Look at http://search.cpan.org/dist/File-Slurp/ , and there are some other 
modules on the CPAN that can do it. (like http://search.cpan.org/dist/IO-All/ 
).

> 
> my $customer_msg_html = $customer_msgStart_html  . OrderFromForm_html()
> . $customer_msgEnd_html . $scalar_sig;

Your style is inconsistent between camelCase, and 
underscore_separated_identifiers. 

> 
> #---- Create Message ---
> 
>     Email::MIME->create(
>         attributes => {
>             content_type => "text/html",
>             charset      => "UTF-8",
>             encoding     => "quoted-printable",
>             format       => "flowed",
>         },
>         body => $customer_msg_html,
>     ),
> 
> 
> ################
> So, as I suggested, the message arrives with the body message all nicely
> concatenated , except for the $scalar_sig variable, which is moissing:
> and I have the 'printFile(FileHandle=GLOB(0x84b66b8))' as a nice fat
> error.
> 
> I would really appreciate your help, and outright suggestions, as I have
> struggled with is and tests for three days now, (but I have got a lot of
> the work done (: -  though this problem has me stumped)!
> 

For more information see:

1. http://perl.net.au/wiki/Freenode_Sharp_Perl_FAQ

2. http://faq.perl.org/

3. http://perl-begin.org/

4. http://www.perl.org/books/beginning-perl/

Regards,

	Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
The Case for File Swapping - http://shlom.in/file-swap

Chuck Norris read the entire English Wikipedia in 24 hours. Twice.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About