Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Printing values from references

by fuzzyping (Chaplain)
on Sep 13, 2003 at 21:57 UTC ( [id://291298]=perlquestion: print w/replies, xml ) Need Help??

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

I know this is probably a noob question, but I just can't figure out why this is happening like it is. I'm simply trying to deref the value of something inside a system() call. When run, the script pulls mail via IMAP, parses the mail for any TIFF images, converts them to postscript, and prints them via lpr. In this example, it prints out the filename instead. The result:
[jason@lappy ~]$ perl parser.pl /tmp/FAX206984492211_9529539900-14.TIF MIME::Entity=HASH(0x80f74a8)->bodyhandle->path
Any idea why the reference in the double-quotes isn't interpreted as I'd expect? I know I've done this sort of thing with other scripts, I assume that I'm just not dealing with the data structure I'd expect (MIME::Parser doesn't document the bodyhandle->path method).
#!/usr/bin/perl use strict; use Net::IMAP::Simple; use MIME::Parser; my $out_dir = "/tmp"; + + my $server = new Net::IMAP::Simple('localhost'); $server->login('testuser','password'); my $parser = MIME::Parser->new; $parser->output_dir($out_dir); + + # Create Archive folder if non-existent $server->select('Archive') || $server->create_mailbox('Archive'); + + # Grab number of messages my $number_of_messages = $server->select('INBOX'); + + # Print messages, copy to Archive, delete message foreach my $msg (1..$number_of_messages) { my $msg_fh = $server->getfh($msg); my $entity = $parser->parse($msg_fh); for my $part ($entity->parts) { if ($part->mime_type =~ /tiff/) { print $part->bodyhandle->path, "\n"; print "$part->bodyhandle->path\n"; } } $entity->purge; #$server->copy($msg,'Archive'); #$server->delete($msg); } + + $server->quit;
-fp

Replies are listed 'Best First'.
Re: Printing values from references
by gryphon (Abbot) on Sep 13, 2003 at 23:17 UTC

    Greetings fuzzyping,

    Pardon the rough nature of this code, but take a look at this:

    #!/usr/bin/perl use strict; use warnings; package Thing; sub stuff { return 'word'; } sub new { return bless {}, shift; } package main; my $thing = new Thing; print $thing->stuff, "\n"; print "$thing->stuff\n"; print $thing, "\n";

    When you put $thing->stuff into quotes, Perl interprets only the $thing instead of the whole line. One solution is to do exactly what you've done which is to not put the $thing->stuff inside quotes. I remember seeing a way to get Perl to interpret the whole $thing->stuff inside a double-quote, but I never use it, so I can't remember the exact syntax right now.

    gryphon
    code('Perl') || die;

      That sounds about right. The only problem is that I can't leave it outside the quotes**, since it's going to be in a system() call for the real script. If anyone can remember how to escape a full reference string like that, I'd be very grateful.

      ** Of course, I can simply read the value into a scalar variable and put that into the system() call, but it seems so inefficient. :)

      -fp

        Perhaps you're thinking of something along the lines of:

        system "frobnitz --oik @{[ $something->method ]}"

        Of course some would consider that uglier than a temporary. Also remember that system() will take a list just fine (presuming you're not dependent on it being passed to a shell for redirection or what not).

        Greetings fuzzyping,

        So why not just do the following?

        system('cmd_line stuff ' . $part->bodyhandle->path . ' and_stuff');

        gryphon
        code('Perl') || die;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found