Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

filter denies existence of input

by hesco (Deacon)
on Aug 22, 2009 at 15:31 UTC ( [id://790580]=perlquestion: print w/replies, xml ) Need Help??

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

I am seeing an error reading: Use of uninitialized value $email in print at /home/hesco/lib/perl/ETC/parse_email.pl line 13., when I invoke a pipe, like this:

cat /home/hesco/lib/perl/ETC/etc/vote2009.mbox | formail -s | /home/he +sco/lib/perl/ETC/parse_email.pl
That code begins like so:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use lib qw( /home/hesco/lib/perl ); use parser; my $email = $ARGV[0]; my($start,$to,$name,$pid,$key); open('EMAIL','>','/tmp/email_throw_away'); print EMAIL $email; # <--- error here ??? close(EMAIL);
and if I pipe that cat output to less instead of through my filter, I get what appears to be an email. How is it that value would be uninitialized?

So next I copied my .mbox file to itself, giving it a new extension of .sample, and edit the .sample so that it contains ONLY a single email, dropped the formail invocation from my pipe and try this:

cat /home/hesco/lib/perl/ETC/etc/vote2009.sample | /home/hesco/lib/per +l/ETC/parse_email.pl
yielding the same result:

Use of uninitialized value $email in print at /home/hesco/lib/perl/ETC +/parse_email.pl line 13.
And now I am really confused.

-- Hugh

UPDATE:

mortiz, zwon: So you are right. This seems to work much better:

cat /home/hesco/lib/perl/ETC/etc/vote2009.mbox | formail -s | /home/he +sco/lib/perl/ETC/parse_email.pl -
Now to figure out why my formail -s is only giving me the first email in the mbox file. Thanks folks!

Never mind on that. formail -s expects the filter as an argument to the -s switch, not to pipe STDOUT to the filter. Issue resolved with:

cat /home/hesco/lib/perl/ETC/etc/vote2009.mbox | formail -s /home/hesc +o/lib/perl/ETC/parse_email.pl -
if( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re: filter denies existence of input
by moritz (Cardinal) on Aug 22, 2009 at 15:43 UTC
    You don't pass any command line parameter to your script, so it's not surprising that $ARGV[0] and thus $email are undef.
    Perl 6 projects - links to (nearly) everything that is Perl 6.
Re: filter denies existence of input
by zwon (Abbot) on Aug 22, 2009 at 15:45 UTC

    Well, you don't pass any arguments to your script, so $ARGV[0] isn't defined.

Re: filter denies existence of input
by dsheroh (Monsignor) on Aug 23, 2009 at 10:25 UTC
    Why are you trying to force your data into being a1 command-line parameter? Normally, piped data is accessed by reading it from STDIN:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use lib qw( /home/hesco/lib/perl ); use parser; open('EMAIL','>','/tmp/email_throw_away'); while (my $email = <STDIN>) { # Or "my $email = <>"[2] my($start,$to,$name,$pid,$key); print EMAIL $email; } close(EMAIL);
    Strictly following the structure of your original code, the open/close probably should have been inside the loop, but that would have resulted in the same file being overwritten on every pass, so I moved them outside, since dumping everything into the same file is more likely to be useful.


    1 Note "a" - singular. You're probably getting multiple messages from formail, but the code you posted only looks at $ARGV[0], which is the first command-line argument, not all arguments. I'm actually mildly surprised that you're even getting the complete first message that way rather than only the first word or first line of the first message.

    2 <STDIN> will, unsurprisingly, read only from STDIN. <> will read the contents of any files specified on the command line, or STDIN if no files are given.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://790580]
Approved by zwon
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-29 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found