Home > Archive > PERL Miscellaneous > May 2005 > Please explain following script for me. Thanks in advance.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Please explain following script for me. Thanks in advance.
|
|
| PerlPerl 2005-05-28, 3:57 pm |
| 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);
}
| |
| ioneabu@yahoo.com 2005-05-28, 3:57 pm |
|
PerlPerl wrote:
> 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
| |
| Fabian Pilkowski 2005-05-28, 3:57 pm |
| Subject: Please explain following script for me. Thanks in advance.
* PerlPerl schrieb:
> 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 $/; <PIPE> };
>
> 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
| |
| Fabian Pilkowski 2005-05-28, 3:57 pm |
| * Fabian Pilkowski schrieb:
> Subject: Please explain following script for me. Thanks in advance.
>
> * PerlPerl schrieb:
>
>
> 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
| |
| PerlPerl 2005-05-28, 8:56 pm |
| 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 <b...@snoopy.apana.org.au>.
# 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);
}
| |
| Fabian Pilkowski 2005-05-28, 8:56 pm |
| * PerlPerl schrieb:
> 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
| |
| John W. Krahn 2005-05-28, 8:56 pm |
| PerlPerl wrote:
> 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 <b...@snoopy.apana.org.au>.
> # May be freely distributed and modified provided copyright stays
> intact.
Did you try contacting the author?
http://lists.debian.org/debian-user...7/msg00862.html
John
--
use Perl;
program
fulfillment
| |
| PerlPerl 2005-05-28, 8:56 pm |
| Fabian,
Sorry if I 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.[color=darkred]
> #!/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
| |
| A. Sinan Unur 2005-05-29, 3:58 am |
| "PerlPerl" <fudongfli3@yahoo.com> wrote in
news:1117321773.571358.225620@g43g2000cwa.googlegroups.com:
> When I commented 'use strict', the error disappeared.
Please read the posting guidelines for this group.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
| xhoster@gmail.com 2005-05-29, 3:58 am |
| Fabian Pilkowski <pilkowsk@informatik.uni-marburg.de> wrote:
> Subject: Please explain following script for me. Thanks in advance.
>
> * PerlPerl schrieb:
>
....[color=darkred]
>
> 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
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
| |
| Henry Law 2005-05-29, 3:56 pm |
| On 28 May 2005 16:09:33 -0700, "PerlPerl" <fudongfli3@yahoo.com>
wrote:
>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.
--
Henry Law <>< Manchester, England
| |
| PerlPerl 2005-05-30, 3:57 am |
| 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
| |
| A. Sinan Unur 2005-05-30, 3:57 am |
| "PerlPerl" <fudongfli3@yahoo.com> wrote in news:1117422978.883907.325340
@g49g2000cwa.googlegroups.com:
> I guess this board might be the wrong one to post questions of
> this kind.
This is not a "board". comp.lang.perl.misc is a UseNet newsgroup.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
| Chris Mattern 2005-05-30, 3:57 pm |
| PerlPerl wrote:
>
> I commented the 'use strict' because it causes errors in following
> code:
You use "use strict" so that it can point out problems with your
code. Deciding that the best way to deal with those problems is
to ignore them does not strike me as the optimal way to proceed.
--
Christopher Mattern
"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
| |
| Jürgen Exner 2005-05-30, 3:57 pm |
| PerlPerl wrote:
> I commented the 'use strict' because it causes errors in following
> code:
Wrong terminology.
use strict; doesn't *cause* errors, it *exposes* them.
If you comment it out then the errors are still there, they are just not
shown to you.
> ###########
> local $k=0;
Shouldn't this by 'my' instead of 'local'?
Actually you can declare $k as part of the 'for' statement see below.
> for($k=0; $k < $num_parts; $k ++) {
for my $k (0..$num_parts-1)
> 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.
No, it doesn't. It just doesn't report it any longer.
"Daddy, the car engine is overheating".
"Well, let's remove the cable to the engine thermometer and then it will be
fine".
jue
| |
| Tad McClellan 2005-05-30, 3:57 pm |
| Jürgen Exner <jurgenex@hotmail.com> wrote:
> PerlPerl wrote:
[color=darkred]
> use strict; doesn't *cause* errors, it *exposes* them.
> If you comment it out then the errors are still there, they are just not
> shown to you.
> "Daddy, the car engine is overheating".
> "Well, let's remove the cable to the engine thermometer and then it will be
> fine".
I like this classic example:
Daddy, the car is making a funny noise.
Well, turn up the volume on the radio.
Just because you cannot hear it does not mean that it has been repaired.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| ioneabu@yahoo.com 2005-05-31, 3:59 am |
|
Tad McClellan wrote:
> J=FCrgen Exner <jurgenex@hotmail.com> wrote:
>
>
>
>
l be[color=darkred]
>
>
> I like this classic example:
>
> Daddy, the car is making a funny noise.
>
> Well, turn up the volume on the radio.
My car once made a funny noise for months and months and nobody had a
clue as to what was causing it. I took it to the dealership, to a
mechanic, to friends and family members who thought they could figure
out anything. Most said I was imagining the noise. The dealership
mechanic drove around with me and insisted that what I heard was from
another car on the road. It was a faint, echoing rattle noise that
only came within a specific speed range of maybe 10-35 mph. I almost
was conviced that it was my imagination. I finally had to get new
tires one day and found the cause of the noise. It was a piece of a
nail that had penetrated the tire and was bouncing around inside the
wheel. It really made an unusual noise and was hard to pinpoint where
it was coming from.
| |
| PerlPerl 2005-05-31, 4:01 pm |
| Many thanks for nail and others. Does anybody know how to send a fax
with perl program? Any help is appreciated.
PerlPerl
ioneabu@yahoo.com wrote:
> Tad McClellan wrote:
not[color=darkred]
ill be[color=darkred]
>
> My car once made a funny noise for months and months and nobody had a
> clue as to what was causing it. I took it to the dealership, to a
> mechanic, to friends and family members who thought they could figure
> out anything. Most said I was imagining the noise. The dealership
> mechanic drove around with me and insisted that what I heard was from
> another car on the road. It was a faint, echoing rattle noise that
> only came within a specific speed range of maybe 10-35 mph. I almost
> was conviced that it was my imagination. I finally had to get new
> tires one day and found the cause of the noise. It was a piece of a
> nail that had penetrated the tire and was bouncing around inside the
> wheel. It really made an unusual noise and was hard to pinpoint where
> it was coming from.
|
|
|
|
|