Please explain following script for me. Thanks in advance.

Discussion in 'Perl Misc' started by PerlPerl, May 28, 2005.

  1. PerlPerl

    PerlPerl Guest

    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);
    }
     
    PerlPerl, May 28, 2005
    #1
    1. Advertisements

  2. PerlPerl

    ioneabu Guest

    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
     
    ioneabu, May 28, 2005
    #2
    1. Advertisements

  3. 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
    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.
    You call your own sub error() if there was an error and success()
    otherwise. But I don't know anything about those subs.
    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, May 28, 2005
    #3
  4. 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
     
    Fabian Pilkowski, May 28, 2005
    #4
  5. PerlPerl

    PerlPerl Guest

    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 <>.
    # 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);
    }
     
    PerlPerl, May 28, 2005
    #5
  6. Whose help? Please quote some context of the posting you're answering
    to, also if you're using Google.
    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?
    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
     
    Fabian Pilkowski, May 28, 2005
    #6
  7. Did you try contacting the author?

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


    John
     
    John W. Krahn, May 28, 2005
    #7
  8. PerlPerl

    PerlPerl Guest

    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.
    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
     
    PerlPerl, May 29, 2005
    #8
  9. Please read the posting guidelines for this group.

    Sinan
     
    A. Sinan Unur, May 29, 2005
    #9
  10. PerlPerl

    xhoster Guest

    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
     
    xhoster, May 29, 2005
    #10
  11. PerlPerl

    Henry Law Guest

    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, May 29, 2005
    #11
  12. PerlPerl

    PerlPerl Guest

    Thanks, John.
    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
     
    PerlPerl, May 30, 2005
    #12
  13. This is not a "board". comp.lang.perl.misc is a UseNet newsgroup.

    Sinan
     
    A. Sinan Unur, May 30, 2005
    #13
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.