karu1975 has asked for the wisdom of the Perl Monks concerning the following question:

hi all i am having problem executing the command system("mailx -s 'title' emailaddr < $file"); it gives me the error with the permissions of that file i:e sh: -rw-r--r--: cannot open . Kindly help is nedded

Replies are listed 'Best First'.
Re: mailx
by sauoq (Abbot) on Jun 19, 2003 at 00:02 UTC

    Sounds like you tried to get the filename into the $file variable by parsing it out of a directory listing. For example, you might have done something like

    for my $file (`ls -l /some/dir`) { ... }
    If you are going to do that, you'll need to improve your parsing. ;-)

    If you show us the code next time, you'll probably get a more thorough response.

    -sauoq
    "My two cents aren't worth a dime.";
    
      Further, if this is the case

      for my $file (`ls -l /some/dir`) { ... }

      then you may be interested in  ls -1, or better still, for a pure Perl method the  readdir function. From perlfunc:

      opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!"; @files= grep { -f "$some_dir/$_" } readdir(DIR); closedir DIR;

      I use the following subroutine to wrap around mailx, which you may find to be a useful starting point

      # # mailit - send an email using mailx. Accepts either a $body or a path + # to a file # # Args: # $subject - email subject # either: # $body - body of email in scalar var, or # $fname - path to file to send. # @emails - list of email addresses to send email to # sub mailit { my ( $subj, $body, $fname, @email ) = @_; my($mailcmd) = "mailx -s\"$subj\""; die "Invalid mailit args\n" unless ($subj and ($body or $fname +)); if ($body) { foreach ( @email ) { open( PI, "|$mailcmd $_" ) or warn "Pipe to ma +ilx failed: $mailcmd\n"; print PI $body; print PI "\n.\n"; close PI; } } elsif ($fname) { die "Bad mailit path $fname\n " unless -e $fname; foreach ( @email ) { `$mailcmd $_ < $fname` or warn "bad mailx resp +onse: $?, $!"; } } }
      Disclaimer: modified for posting, so untested.

      mailit(), in conjunction with readdir, should do what you want.

      Hope this helps, bm.