Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

poj's scratchpad

by poj (Priest)
on Jun 12, 2013 at 17:16 UTC ( #1038520=scratchpad: print w/ replies, xml ) Need Help??

my $osname = $^O; if( $osname eq 'MSWin32' ){{ use lib('c:/path/on/windows'); } else { use lib('/path/on/unix'); }
--------------------------------------------
#!/usr/bin/perl -w use strict; use warnings; use PDF::API2; use PDF::API2::Annotation; use PDF::Create; use PDF::Table; use Time::Format;# qw(%time %strftime %manip); use File::Copy; use Cwd qw( realpath ); use File::Basename qw( dirname ); # Look for modules in the same dir as the script. use lib dirname(realpath($0)); use MyDBConn qw(get_dbh_sercm get_dbh_percm get_dbh_ercmt get_dbh_zerc +m); use MySendEmail qw(send_eml_RecUpdtError); my $ORACLE_HOME = "/u1/app/oracle/product/11.2.0/client_1"; my $ORACLE_SID="SERCM"; my $LD_LIBRARY_PATH="$ORACLE_HOME/lib";

Add these lines to your script

#!perl use Cwd qw( realpath ); use File::Basename qw( dirname ); # Look for modules in the same dir as the script. use lib dirname(realpath($0));
=================================================== #!/usr/bin/perl use strict; use warnings; my $now = scalar localtime; print "Content-Type: text/html\n\n"; print <<HTML <html> <head> <title>Test CGI</title> </head> <body> <h1>Perl works !</h1> <b>Time : $now</b> </body> </html> HTML
-------------------------------------
#!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp 'fatalsToBrowser'; my $now = scalar localtime; my $cgi = CGI->new(); print "Content-Type: text/html\n\n"; print <<HTML <html> <head> <title>Test CGI</title> </head> <body> <h1>Perl CGI works !</h1> <b>Time : $now</b> </body> </html> HTML
============================================
http://stackoverflow.com/questions/5293246/why-the-1-at-the-end-of-each-perl-package

Will our @EXPORT_OK = qw(send_eml_NDF send_eml_RecUpdtError); help me call both subs in my script?
Yes, but at the top of your script you need the statement
use MySendEmail qw(send_eml_NDF send_eml_RecUpdtError);

==============================================

The only 2 valid array names are @EXPORT or @EXPORT_OK. If you create your own variation they will not do anything. They determine the subroutines that are 'imported' into your script when you use MyModule.

If you have @EXPORT = qw(a b c) in your module then use MyModule will import sub a, sub b and sub c into your script. This is not normally done because unless you look at the module you don't know what sub names have been used in the module and there is a risk of duplication with a sub name you have in your script

The safe way is to use @EXPORT_OK = qw(a b c) and then use MyModule does not import anything by default, each sub you want to use has to be listed. For example use MyModule qw(a c) would only import sub a and sub c, not sub b

see mark-up #### below
##MySendEmail.pm package MySendEmail; use strict; use Email::MIME; use Email::Sender::Simple qw(sendmail); use parent qw(Exporter); #### our @EXPORT_NDF = qw/ send_eml_NDF /; #### our @EXPORT_RecUPDError = qw/ send_eml_RecUpdtError /; our @EXPORT_OK = qw(send_eml_NDF send_eml_RecUpdtError); sub send_eml_NDF { # first, create your message my $message = Email::MIME->create( header_str => [ From => 'Fromsomeone@abc.com', To => 'Tosomeone@abc.com', CC => 'CCsomeone@abc.com',, Subject => 'ERROR - No Data Found for PDF Letters', ], attributes => { encoding => 'quoted-printable', charset => 'ISO-8859-1', }, body_str => "There has been an error in generating letters for to +day because query has no data found. Please contact IT Team.", ); #### use Email::Sender::Simple qw(sendmail); <-- move to top return sendmail($message); } sub send_eml_RecUpdtError { # first, create your message #### my (@v_acc_num) = @_; my ($v_acc_num) = @_; # <--- chg here and in body_str my $message = Email::MIME->create( header_str => [ From => 'Fromsomeone@abc.com', To => 'Tosomeone@abc.com', CC => 'CCsomeone@abc.com', Subject => 'ERROR Generating PDF Letters', ], attributes => { encoding => 'quoted-printable', charset => 'ISO-8859-1', }, body_str => "There was an error updating EXTRACT_TIMESTAMP column +for the account number: $v_acc_num", ); #### use Email::Sender::Simple qw(sendmail); <-- move to top return sendmail($message); } 1; #### <--- add this
------------------------------------------------

Here's my version, is this located in module or script ?

#!perl my $FILESIZE = 100 * 1024; my $PATH = '/path/to/files/'; my $UPD = 'sql here'; # check file exists and greater than 100k sub check { my ($acc_number,$pdfname,$dbh) = @_; my $file = $PATH.$pdfname; rename($file,$PATH.'test_change.pdf'); # test # check exists my $error=0; if (-e $file){ # check size my $size = -s $file; if ($size < $FILESIZE){ my $sth = $dbh->prepare($UPD); $sth->execute($acc_number) or die $sth->rollback(); print "\n\n$acc_number --- FILE SIZE IS : $size\n"; $error=1; } } else { # file not exists print "\n\n$acc_numeber --- FILE NOT FOUND : $file\n"; $error=1; } send_eml_RecUpdtError($acc_number) if ($error); }
---------------------------------------------

To get variables into the sub use my ($var1,$var2,$var3) = @_; or just my $var1 = shift if only 1 para. Only problem with this is if you add more parameters later and forget to change shift to @_ !!


package MySendEmail; use strict; use Email::MIME; use Email::Sender::Simple qw(sendmail); use parent qw/ Exporter /; our @EXPORT = qw/ send_eml_NDF send_eml_PDFGenError send_eml_RecUpdtError /; sub send_eml_RecUpdtError{ # my $account_no = shift; # this would also be OK my ($account_no) = @_; # first, create your message my $message = Email::MIME->create( header_str => [ From => 'Fromsomeone@abc.com>', To => 'Tosomeone@abc.com', CC => 'CCsomeone@abc.com', Subject => 'ERROR Generating PDF Letters', ], attributes => { encoding => 'quoted-printable', charset => 'ISO-8859-1', }, body_str => "There was an error updating EXTRACT_TIMESTAMP column +for the account number:".$account_no, ); return sendmail($message); } 1;

To call the sub

#!perl use strict; use MySendEmail; # send email send_eml_RecUpdtError( '1235' );

I'm not sure what fault you are anticipating but my worry would be generating a large number of emails, maybe because a folder permission was changed so all the pdf's failed. I would think about a single email at the end that either said OK no faults, or a list of account no's that failed.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (17)
As of 2015-03-05 16:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:









    Results (147 votes), past polls