perl guff :-)

Discussion in 'NZ Computing' started by Shane, Aug 11, 2005.

  1. Shane

    Shane Guest

    Im bored and Ive been writing perl scripts to help me with the mail :)
    I have written a script that checks for mail in several directorys, and
    then emails me the names of the directorys that have unread mail in them
    however, its sending me mail when there is no unread mail,
    Ive worked out where to test, and in which subroutine, but I cant figure
    out how to 'kill' it
    if I use 'die' it returns an error to stdin which isnt helpful
    any ideas?




    ps heres part of the code
    sub searcher{
    #@folder = </home/vmail/weasel.is-a-geek.net/shane/.Asterisk.*>;
    @folder = </home/baldrick/test/*>;
    foreach $folders(@folder){
            @maildirs = <$folders/cur/*>;
            foreach $mail(@maildirs){
                    if ($mail!~/S$/){
                    ($folders=~s"/home/vmail/weasel.is-a-geek.net/shane/"");
                    push (@group, $folders."\n");}}}
                    if (@group<=0){die}
                    return @group;}

    Ta Nicely :)

    --
    Hardware, n.: The parts of a computer system that can be kicked

    The best way to get the right answer on usenet is to post the wrong one.
    Shane, Aug 11, 2005
    #1
    1. Advertising

  2. Shane

    Chris Hope Guest

    Shane wrote:

    [snip]

    Been a few years since I did any perl coding but...

    > ps heres part of the code
    > sub searcher{
    > #@folder = </home/vmail/weasel.is-a-geek.net/shane/.Asterisk.*>;
    > @folder = </home/baldrick/test/*>;
    > foreach $folders(@folder){
    > @maildirs = <$folders/cur/*>;
    > foreach $mail(@maildirs){
    > if ($mail!~/S$/){
    > ($folders=~s"/home/vmail/weasel.is-a-geek.net/shane/"");
    > push (@group, $folders."\n");}}}
    > if (@group<=0){die}


    can you not do a 'return' here instead of 'die'?

    --
    Chris Hope | www.electrictoolbox.com | www.linuxcdmall.co.nz
    Chris Hope, Aug 11, 2005
    #2
    1. Advertising

  3. Shane

    Harry Guest

    Shane wrote:

    > Im bored and Ive been writing perl scripts to help me with the mail :)
    > I have written a script that checks for mail in several directorys, and
    > then emails me the names of the directorys that have unread mail in them
    > however, its sending me mail when there is no unread mail,
    > Ive worked out where to test, and in which subroutine, but I cant figure
    > out how to 'kill' it
    > if I use 'die' it returns an error to stdin which isnt helpful
    > any ideas?
    >
    >
    >
    >
    > ps heres part of the code
    > sub searcher{
    > #@folder = </home/vmail/weasel.is-a-geek.net/shane/.Asterisk.*>;
    > @folder = </home/baldrick/test/*>;
    > foreach $folders(@folder){
    > @maildirs = <$folders/cur/*>;
    > foreach $mail(@maildirs){
    > if ($mail!~/S$/){
    > ($folders=~s"/home/vmail/weasel.is-a-geek.net/shane/"");
    > push (@group, $folders."\n");}}}
    > if (@group<=0){die}
    > return @group;}
    >


    Well, apart from the obvious errors, shouldn't you be using stat to check
    the time an existing file was last modified? Just checking for its
    existence is not good enough.

    I don't understand what the second-to-last line is trying to do.
    Harry, Aug 11, 2005
    #3
  4. Shane

    Shane Guest

    On Thu, 11 Aug 2005 21:33:34 +1000, Harry wrote:

    > Shane wrote:
    >
    >> Im bored and Ive been writing perl scripts to help me with the mail :)
    >> I have written a script that checks for mail in several directorys, and
    >> then emails me the names of the directorys that have unread mail in them
    >> however, its sending me mail when there is no unread mail, Ive worked
    >> out where to test, and in which subroutine, but I cant figure out how to
    >> 'kill' it
    >> if I use 'die' it returns an error to stdin which isnt helpful any
    >> ideas?
    >>
    >>
    >>
    >>
    >> ps heres part of the code
    >> sub searcher{
    >> #@folder = </home/vmail/weasel.is-a-geek.net/shane/.Asterisk.*>; @folder
    >> = </home/baldrick/test/*>;
    >> foreach $folders(@folder){
    >> @maildirs = <$folders/cur/*>;
    >> foreach $mail(@maildirs){
    >> if ($mail!~/S$/){
    >> ($folders=~s"/home/vmail/weasel.is-a-geek.net/shane/""); push (@group,
    >> $folders."\n");}}}
    >> if (@group<=0){die}
    >> return @group;}
    >>
    >>

    > Well, apart from the obvious errors, shouldn't you be using stat to check
    > the time an existing file was last modified? Just checking for its
    > existence is not good enough.
    >
    > I don't understand what the second-to-last line is trying to do.


    the line 'if ($mail!~/S$/){' checks if the mail has been read (imap mail
    has a capital S appended to its filename when it is marked as read)
    the second to last line 'if (@group<=0){die}' is supposed to check if any
    elements have been added to that array and if none at all I want the whole
    process to die
    I have decided to change that line to
    if (@group<=0){@group=("Bonneville");}
    so that I can check the return from the sub routine and if it eq
    "Bonneville" then I wont enter the loop that emails me

    I will be using stat in another function to check if any mail has been
    sent at all
    Ta


    --
    Hardware, n.: The parts of a computer system that can be kicked

    The best way to get the right answer on usenet is to post the wrong one.
    Shane, Aug 11, 2005
    #4
  5. Shane

    Shane Guest

    On Thu, 11 Aug 2005 21:27:36 +1200, Chris Hope wrote:

    > Shane wrote:
    >
    > [snip]
    >
    > Been a few years since I did any perl coding but...
    >
    >> ps heres part of the code
    >> sub searcher{
    >> #@folder = </home/vmail/weasel.is-a-geek.net/shane/.Asterisk.*>; @folder
    >> = </home/baldrick/test/*>;
    >> foreach $folders(@folder){
    >> @maildirs = <$folders/cur/*>;
    >> foreach $mail(@maildirs){
    >> if ($mail!~/S$/){
    >> ($folders=~s"/home/vmail/weasel.is-a-geek.net/shane/""); push (@group,
    >> $folders."\n");}}}
    >> if (@group<=0){die}

    >
    > can you not do a 'return' here instead of 'die'?


    in the end I decided to return a crap value if @group was empty, and in
    the loop that checks the return of this value I changed to
    @test=&searcher
    if (@test ne "Bonneville") {then email me the results}


    ta

    --
    Hardware, n.: The parts of a computer system that can be kicked

    The best way to get the right answer on usenet is to post the wrong one.
    Shane, Aug 11, 2005
    #5
  6. Shane

    Harry Guest

    Shane wrote:

    > On Thu, 11 Aug 2005 21:33:34 +1000, Harry wrote:
    >
    >> Shane wrote:
    >>
    >>> Im bored and Ive been writing perl scripts to help me with the mail :)
    >>> I have written a script that checks for mail in several directorys, and
    >>> then emails me the names of the directorys that have unread mail in them
    >>> however, its sending me mail when there is no unread mail, Ive worked
    >>> out where to test, and in which subroutine, but I cant figure out how to
    >>> 'kill' it
    >>> if I use 'die' it returns an error to stdin which isnt helpful any
    >>> ideas?
    >>>
    >>>
    >>>
    >>>
    >>> ps heres part of the code
    >>> sub searcher{
    >>> #@folder = </home/vmail/weasel.is-a-geek.net/shane/.Asterisk.*>; @folder
    >>> = </home/baldrick/test/*>;
    >>> foreach $folders(@folder){
    >>> @maildirs = <$folders/cur/*>;
    >>> foreach $mail(@maildirs){
    >>> if ($mail!~/S$/){
    >>> ($folders=~s"/home/vmail/weasel.is-a-geek.net/shane/""); push (@group,
    >>> $folders."\n");}}}
    >>> if (@group<=0){die}
    >>> return @group;}
    >>>
    >>>

    >> Well, apart from the obvious errors, shouldn't you be using stat to check
    >> the time an existing file was last modified? Just checking for its
    >> existence is not good enough.
    >>
    >> I don't understand what the second-to-last line is trying to do.

    >
    > the line 'if ($mail!~/S$/){' checks if the mail has been read (imap mail
    > has a capital S appended to its filename when it is marked as read)


    Not so on most standard unix/linux.
    Must be something to do with the vmail you are using.

    > the second to last line 'if (@group<=0){die}' is supposed to check if any
    > elements have been added to that array and if none at all I want the whole
    > process to die


    Why don't you try "$#group"?

    > I have decided to change that line to
    > if (@group<=0){@group=("Bonneville");}
    > so that I can check the return from the sub routine and if it eq
    > "Bonneville" then I wont enter the loop that emails me
    >
    > I will be using stat in another function to check if any mail has been
    > sent at all


    Vanilla unix/linux mailbox just modifies each Status header with something
    like "RO" to indicate whether a mail item has been read.
    In which case you need to read the entire /var/spool/mail/fred file
    and parse each set of header.
    Harry, Aug 12, 2005
    #6
  7. Shane

    Shane Guest

    this is quickly degenerating into a thread that has nothing to do with my
    original question
    what may or may not happen to mail elsewhere is hardly my concern when
    what IS happening to my mail is the concern of my script
    thanks

    --
    Hardware, n.: The parts of a computer system that can be kicked

    The best way to get the right answer on usenet is to post the wrong one.
    Shane, Aug 12, 2005
    #7
  8. Shane

    Enkidu Guest

    Harry wrote:
    >
    > Vanilla unix/linux mailbox just modifies each Status
    > header with something like "RO" to indicate whether a
    > mail item has been read. In which case you need to
    > read the entire /var/spool/mail/fred file
    > and parse each set of header.
    >

    It might be mbox, or it might be maildir. I've never had to
    delve that deeply, but I vaguely recall something about
    maildir.....

    Cheers,

    Cliff

    --

    Barzoomian the Martian - http://barzoomian.blogspot.com
    Enkidu, Aug 12, 2005
    #8
  9. Shane

    Harry Guest

    Shane wrote:

    > this is quickly degenerating into a thread that has nothing to do with my
    > original question
    > what may or may not happen to mail elsewhere is hardly my concern when
    > what IS happening to my mail is the concern of my script
    > thanks
    >


    Ok, so what do you mean by "kill it"?
    BTW "die" doesn't return anything to stdin, it outputs its argument
    to stderr and causes exit.
    Harry, Aug 12, 2005
    #9
  10. Shane

    Shane Guest

    On Fri, 12 Aug 2005 18:50:04 +1000, Harry wrote:

    > Shane wrote:
    >
    >> this is quickly degenerating into a thread that has nothing to do with
    >> my original question
    >> what may or may not happen to mail elsewhere is hardly my concern when
    >> what IS happening to my mail is the concern of my script thanks
    >>
    >>

    > Ok, so what do you mean by "kill it"? BTW "die" doesn't return anything to
    > stdin, it outputs its argument to stderr and causes exit.



    At the time I meant.. the loop to end, more so I want the loop that called
    this subroutine to end,
    IOW my poorly written script is supposed to email me a list of folders
    when it detects 'unread' mail in any of those folders, however if it finds
    none, I dont want it emailing me
    What Ive done is, if nothing meets its criteria, it now returns a crap
    value (in this case "Bonneville") and the original loop checks for this
    value, and then mails me if it has been returned anything ne "Bonneville"
    if (@group<=0){@group=("Bonneville")}

    my @check=&searcher
    if ($check[0]!~m/Bonneville){send me an email}

    and yeah I said STDERR :)


    --
    Hardware, n.: The parts of a computer system that can be kicked

    The best way to get the right answer on usenet is to post the wrong one.
    Shane, Aug 12, 2005
    #10
  11. Shane

    Harry Guest

    Shane wrote:

    > On Fri, 12 Aug 2005 18:50:04 +1000, Harry wrote:
    >
    >> Shane wrote:
    >>
    >>> this is quickly degenerating into a thread that has nothing to do with
    >>> my original question
    >>> what may or may not happen to mail elsewhere is hardly my concern when
    >>> what IS happening to my mail is the concern of my script thanks
    >>>
    >>>

    >> Ok, so what do you mean by "kill it"? BTW "die" doesn't return anything
    >> to stdin, it outputs its argument to stderr and causes exit.

    >
    >
    > At the time I meant.. the loop to end, more so I want the loop that called
    > this subroutine to end,
    > IOW my poorly written script is supposed to email me a list of folders
    > when it detects 'unread' mail in any of those folders, however if it finds
    > none, I dont want it emailing me
    > What Ive done is, if nothing meets its criteria, it now returns a crap
    > value (in this case "Bonneville") and the original loop checks for this
    > value, and then mails me if it has been returned anything ne "Bonneville"
    > if (@group<=0){@group=("Bonneville")}
    >
    > my @check=&searcher
    > if ($check[0]!~m/Bonneville){send me an email}
    >
    > and yeah I said STDERR :)
    >
    >


    Why don't you just check for an empty array?
    As in "return @group" then the caller tests:

    my @group = searcher();
    last if "$#group < 0"; # or whatever

    I think you should read your Perl guide again.
    Harry, Aug 13, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rgarvind

    Regarding perl programming. How 2 make HTTP request?

    rgarvind, Jan 10, 2005, in forum: Microsoft Certification
    Replies:
    1
    Views:
    699
  2. mkinsman

    NAS Perl/CGI Script

    mkinsman, Nov 23, 2003, in forum: Cisco
    Replies:
    0
    Views:
    581
    mkinsman
    Nov 23, 2003
  3. Rick Frink
    Replies:
    0
    Views:
    1,746
    Rick Frink
    Aug 6, 2004
  4. business one way

    Is it a Perl program or a Perl script?

    business one way, Jan 5, 2008, in forum: Digital Photography
    Replies:
    0
    Views:
    732
    business one way
    Jan 5, 2008
  5. Dave - Dave.net.nz

    more apple on intel guff.

    Dave - Dave.net.nz, Jun 14, 2005, in forum: NZ Computing
    Replies:
    0
    Views:
    339
    Dave - Dave.net.nz
    Jun 14, 2005
Loading...

Share This Page