Please explain following script for me. Thanks in advance.

P

PerlPerl

if (open(PIPE, "-|")) {
my $oldsep = $/;
undef($/);
my $msg = <PIPE>;
$/ = $oldsep;
close(PIPE);




if ($? != 0) {
my $exit_value = $? >> 8;
my $signal_num = $? & 127;
my $dumped_core = $? & 128;




error("system faxspool $phone failed: exit=$exit_value, ".
"signal=$signal_num, core=$dumped_core\n\n$msg");
} else {
success("Output from faxspool:\n\n$msg");
}
} else {
my $rc = open(STDERR, ">&STDOUT");
if (!$rc) {
print("Can't dup stdout");
die("Can't dup stdout");
}
exec("faxspool","-F","postmast-er",$phone,@files);
}
 
I

ioneabu

PerlPerl said:
if (open(PIPE, "-|")) {
my $oldsep = $/;
undef($/);
my $msg = <PIPE>;
$/ = $oldsep;
close(PIPE);




if ($? != 0) {
my $exit_value = $? >> 8;
my $signal_num = $? & 127;
my $dumped_core = $? & 128;




error("system faxspool $phone failed: exit=$exit_value, ".
"signal=$signal_num, core=$dumped_core\n\n$msg");
} else {
success("Output from faxspool:\n\n$msg");
}
} else {
my $rc = open(STDERR, ">&STDOUT");
if (!$rc) {
print("Can't dup stdout");
die("Can't dup stdout");
}
exec("faxspool","-F","postmast-er",$phone,@files);
}

It is a perl script that compiles, but does not execute due to an
undefined subroutine. Adding to the top:

use strict;
use warnings;

causes it not to compile due to additional errors.

wana
 
F

Fabian Pilkowski

Subject: Please explain following script for me. Thanks in advance.

* PerlPerl said:
if (open(PIPE, "-|")) {

You open's a pipe. Read `perldoc perlopentut` for details.
my $oldsep = $/;
undef($/);
my $msg = <PIPE>;
$/ = $oldsep;
close(PIPE);

You read all data from PIPE in slurp mode. Btw, you could replace these
five lines by only one statement: my $msg = do { local $/; said:
if ($? != 0) {
my $exit_value = $? >> 8;
my $signal_num = $? & 127;
my $dumped_core = $? & 128;

You want to know if there was an error thus far. Please read the section
about "$?" in `perldoc perlvar` to see why you need those bit operators
here.
error("system faxspool $phone failed: exit=$exit_value, ".
"signal=$signal_num, core=$dumped_core\n\n$msg");
} else {
success("Output from faxspool:\n\n$msg");
}

You call your own sub error() if there was an error and success()
otherwise. But I don't know anything about those subs.
} else {
my $rc = open(STDERR, ">&STDOUT");
if (!$rc) {
print("Can't dup stdout");
die("Can't dup stdout");
}
exec("faxspool","-F","postmast-er",$phone,@files);
}

If opening the pipe fails you try to dup STDOUT. If that fails either
you call die() to terminate with an appropriate message. Otherwise you
call exec() to execute "faxspool".

regards,
fabian
 
F

Fabian Pilkowski

* Fabian Pilkowski said:
Subject: Please explain following script for me. Thanks in advance.



You open's a pipe. Read `perldoc perlopentut` for details.


You read all data from PIPE in slurp mode. Btw, you could replace these
five lines by only one statement: my $msg = do { local $/; <PIPE> };

Err, I want to fix a mistake I've done here. Closing the PIPE is not
included in this statement. One has to do this explicitly in this case.

regards,
fabian
 
P

PerlPerl

Really appreciate your help. I was actually working on a piece of code
(copied from somewhere) to send emails to fax. It is not working for
me. Please take a look for me why it is not working.

Here is the complete code:

#!/usr/bin/perl


#use strict;
use MIME::parser;
use POSIX;




# (C)opyright 2000 Brian May <[email protected]>.
# May be freely distributed and modified provided copyright stays
intact.




# Requires:
# MIME-tools-4.124
# Mail-tools-1.13
# IO-stringy-1.207
# in addition to packages provided with Perl, Debian Potato.




# these can be found at CPAN sites.


print "Content-Type: text/html\n\n";
my $tmpdir = POSIX::tmpnam();
mkdir($tmpdir,0700) or die "Cannot create $tmpdir";


END {
system("rm","-rf",$tmpdir);
}




# Parse command line parameters
my $phone = shift;
die "No phone number given" if (!defined($phone));


my $parser = new MIME::parser;
$parser->output_dir($tmpdir);
my $entity = $parser->read(\*STDIN) or die "couldn't parse MIME
stream";




my $head = $entity->head;




my $subject = $head->get('Subject',0);
chomp($subject);
#error("Invalid Password") if ($subject ne "obvious");




process($entity);




process_all();
exit 0;




my @files = ();




my $level =0;




sub process($) {
my $entity = shift;




$level++;


if($entity->effective_type eq "multipart/mixed") {
process_multipart_mixed($entity);
} elsif ($entity->effective_type eq "text/plain") {
process_part($entity);
} else { error("Unknown MIME type: ".$entity->effective_type); }
$level--;
}




sub process_multipart_mixed() {
my $entity = shift;


my $num_parts = $entity->parts;
local $k=0;
for($k=0; $k < $num_parts; $k ++) {
my $part = $entity->parts($k);
process($part);
}






}




sub process_part() {
my $entity = shift;
my $body = $entity->bodyhandle();


if (!defined($body->path)) {
error("file not written to disk");
} else {
push @files,$body->path;
}




sub process_all() {


if (open(PIPE, "-|")) {
my $oldsep = $/;
undef($/);
my $msg = <PIPE>;
$/ = $oldsep;
close(PIPE);




if ($? != 0) {
my $exit_value = $? >> 8;
my $signal_num = $? & 127;
my $dumped_core = $? & 128;




error("system faxspool $phone failed: exit=$exit_value, ".
"signal=$signal_num, core=$dumped_core\n\n$msg");
} else {
success("Output from faxspool:\n\n$msg");
}
} else {
my $rc = open(STDERR, ">&STDOUT");
if (!$rc) {
print("Can't dup stdout");
die("Can't dup stdout");
}
exec("faxspool","-F","postmast-er",$phone,@files);
}






}


sub error() {
my $error = shift;


my $message =
"Sorry - your fax could not be sent as the following error occured:\n".
"\n".
"$error\n".
"\n";




print "Sending fax to $phone failed\n".$message;
exit(0);


}




sub success() {
my $error = shift;
my $message = "Your fax has been spooled and should be sent
shortly.\n". "\n". "$error\n". "\n";
sendmail("Fax spooled for $phone",$message);
exit(0);






}




sub sendmail {
my $subject =shift;
my $message =shift;


local $SIG{PIPE} = sub { die "Pipe to sendmail broke" };






open(PIPE,"| formail -rkb -I'Subject: $subject' | /usr/lib/sendmail
-t")
or die "Cannot open pipe to send mail: $!\n";




print(PIPE $head->as_string) or die "Cannot print message to pipe:
$!\n";
print(PIPE "\n") or die "Cannot print message to pipe: $!\n";
print(PIPE $message) or die "Cannot print message to pipe: $!\n";
close(PIPE) or die "Cannot close pipe to send mail: $!\n";
exit(0);
}
 
F

Fabian Pilkowski

* PerlPerl said:
Really appreciate your help.

Whose help? Please quote some context of the posting you're answering
to, also if you're using Google.
I was actually working on a piece of code
(copied from somewhere) to send emails to fax. It is not working for
me. Please take a look for me why it is not working.

No, I don't. This group is for helping you if you have problems with
programming Perl. But you don't program in Perl -- you just want to use
it, don't you? I assume this due to your lack of some basics in your
first posting in this thread.

You simply post your whole script here -- just saying that it doesn't
work. You have to say what is not working in order that we could help
you. And please, don't post 200 and more lines of code. Reduce your
script to the minimum needed to show and reproduce the part you have
problems with.

No one (who really wants to learn a language) has problems with a whole
program -- rather than a specific part of it (in comparison with spoken
languages you can think of this part as "vocables" or "terms"). Since
this newsgroup is only for those who are occupied with perl, you have to
show a little more interest in solving your problem. What have you tried
so far? How do the error messages read?
Here is the complete code:

#!/usr/bin/perl

#use strict;
use MIME::parser;
use POSIX;

Could you explain why there is a #-sign in front of "use strict"? This
is not what it is intended for. Fix this first (and hope your program
still compiles afterwards)!

regards,
fabian
 
J

John W. Krahn

PerlPerl said:
Really appreciate your help. I was actually working on a piece of code
(copied from somewhere) to send emails to fax. It is not working for
me. Please take a look for me why it is not working.

Here is the complete code:

#!/usr/bin/perl

#use strict;
use MIME::parser;
use POSIX;

# (C)opyright 2000 Brian May <[email protected]>.
# May be freely distributed and modified provided copyright stays
intact.

Did you try contacting the author?

http://lists.debian.org/debian-user/2000/07/msg00862.html


John
 
P

PerlPerl

Fabian,
Sorry if I confused or offended you. Yes, as you said and I admit, I
only use perl and have been used it in web programming for six years.
Since my background is biology, the basics of perl has been a myth for
me. The script does not work. I mean, it does not send information to
my fax machine. Also when I checked the code, I don't understand how it
works, how it sends information to a fax machine though it compiles and
sends email to me.
#!/usr/bin/perl
#use strict;
use MIME::parser;
use POSIX;
I commented the 'use strict' because it causes errors in following
code:
###########
local $k=0;
for($k=0; $k < $num_parts; $k ++) {
my $part = $entity->parts($k);
process($part);
}
###################
Its error message is as follows:
Global symbol "$k" requires explicit package name at fax.cgi line 80.
When I commented 'use strict', the error disappeared.

Best Regards,

PerlPerl
 
X

xhoster

Fabian Pilkowski said:
Subject: Please explain following script for me. Thanks in advance.



If opening the pipe fails you try to dup STDOUT.

Yes, this applies if the open fails. But much more importantly, it
also applies if the open succeeds. He is using a forking open, which
means the parent process gets a true value and the child gets a false
value (0). So that means the child sends the STDERR from the exec
back to the parent (thorugh the parent's PIPE filehandle).

Xho
 
H

Henry Law

I commented the 'use strict' because it causes errors in following
code: (code snipped)
Its error message is as follows:
Global symbol "$k" requires explicit package name at fax.cgi line 80.
When I commented 'use strict', the error disappeared.

Do you replace the mains fuses in your house with paperclips, too?

"strict" is there to help you fix problems, so you disable it and then
come here looking for help to fix your problem. Hmm.
 
P

PerlPerl

Thanks, John.
Did you try contacting the author?
I sent an email to Brian May, the owner of this program. Hope he will
reply me soon. I really need to make this program work, otherwise may
have to go to those email/fax services companies. I guess this board
might be the wrong one to post questions of this kind. I have searched
through google, but did not find very much information. This program is
the only clue I have. Maybe impossible.
By the way, I fixed the 'use strict' problem. As sombody said, I only
use Perl, so I did not care very much this kind thing if it worked.
Guess I have to change now.

Best Regards,

PerlPerl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top