Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Chapter 8 of intermediate perl

by actualize (Monk)
on Jun 26, 2008 at 00:51 UTC ( #694078=perlquestion: print w/ replies, xml ) Need Help??
actualize has asked for the wisdom of the Perl Monks concerning the following question:

Hey all. I got stuck on a quetion in "Intermediate Perl" by Randal L. Schwartz, brian d, foy & Tom Phoenix. So I went over the answer in the back.

I am pretty sure I understand how the problem is supposed to work but I don't quit understand why the code won't run on my machine. Can anyone enlighten me?

Code:
#!/usr/bin/perl -w #distlog.pl use strict; use IO::File; my %output_handles; while (<>) { unless (/^(\S+):/) { warn "ignoring the line with missing name: $_"; next; } my $name = lc $1; my $handle = $output_handles{$name} ||= IO::File->open(">$name.info") || die "Cannot create $name.info: $!"; print $handle $_; }
Error: Can't use string ("IO::File") as a symbol ref while "strict refs" in u +se at /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/IO/File.pm lin +e 188, <> line 1.

Thanks,

-Actualize

Comment on Chapter 8 of intermediate perl
Select or Download Code
Re: Chapter 8 of intermediate perl
by kyle (Abbot) on Jun 26, 2008 at 01:44 UTC

    It's been a while since I used IO::File, but it appears that open() is not a class method but an instance method. You should instead do IO::File->new( ">$name.info" )

Re: Chapter 8 of intermediate perl
by ikegami (Pope) on Jun 26, 2008 at 02:19 UTC
    Your question has already been answered, but I gotta wonder why you're using IO::File at all. Ever since 5.6 (March 2000), you can get lexical file handles from the open function.
    if (!$output_handles{$name}) { open($output_handles{$name}, '>', "$name.info") or die "Cannot create $name.info: $!\n"; } my $handle = $output_handles{$name}; print $handle $_;

    Update: Fixed bug.

      I guess the only reason is because the book may be a bit out of date. This book really focuses on how to use references. They probably created this case to serve as an example. In this chapter it is about using references with File Handles. I think that they were using this case as an example of how to create file handles on the fly with references.

      I am not sure why this IO::File is used in this case. They introduce it only as a subclass of IO::Handle and give some examples on how to use it.

      Thanks for the help guys,

      -Actualize

        Handles created by open are also IO::Handle objects. All IO handles are, actually. You just need to load IO::Handle (use IO::Handle;) to use the methods.

        use IO::Handle; open(my $fh, '>', $fn) or die; print $fh 'foo!'; $fh->flush(); # From IO::Handle

        Chapter 8 in Intermediate Perl is about "Filehandle References", and it's current up to 5.8. We cover lexical filehandles and the three argument open, but we also cover the IO::Handle modules too. We have three exercises for that chapter, so we use a different technique for each answer.

        As with all of our answers, it's only one way to do it. And, as with any teaching environment, you don't necessarily do things as you would in real life. Once you know the technique, you can forget it and use the one you like best. In this case, it would be the three-argument open in the section "An Even Better Way" :)

        --
        brian d foy <brian@stonehenge.com>
        Subscribe to The Perl Review

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-10-23 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (129 votes), past polls