LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-17-2010, 01:14 PM   #1
MheAd
Member
 
Registered: Jun 2007
Distribution: Ubuntu 14.04
Posts: 186

Rep: Reputation: 36
Problem accessing filehandles (Perl).


Hi guys,
I've been reading / trying to learn some Perl lately. I've found the free book, "Learning Perl", quite good and easy to follow most of the times. However, today I was testing one of the scripts in the book and it would only run partly.

Code:
#!/usr/bin/perl 

use warnings;
use strict;

my %inventory;
print "Enter individual items, followed by a new line.\n";
print "Enter a blank line to finish.\n";

while (1) {
        my $item = <STDIN>;
        chomp $item;
        last unless $item;
        $inventory{lc $item}++;
}

open (SORT, "| perl sort.plx") or *SORT = *STDOUT;
select *SORT;

while (my ($item, $quantity) = each %inventory) {
        if ($quantity > 1) {
                $item =~ s/^(\w+)\b/$1s/ unless $item =~ /^\w+s\b/;
        }
        print "$item: $quantity\n";
}
The problem is the open(SORT...) row.
This row is supposed to give option, either to use an additional script as pipe (sort.plx), or STDOUT if the extra script is not found.

It works with the other script present, however, with the sort.plx being absent it simply wont use STDOUT instead but whines to me:

Can't open perl script "sort.plx": No such file or directory

Has the author of the original script done some error writing it? Since I'm a beginner and found most of the stuff confusing in Perl either way (having previously only experience in Bash and some in PHP) - I can't really tell for sure , but from what I can read in documentation - it doesn't appear to be wrong.

As it looks now, the only way for me to use STDOUT is only by removing:
open (SORT, "| perl sort.plx") or *SORT = *STDOUT;
select *SORT;

rows. But then, I lose the piping in to that other script as an option.

Any ideas? Thanks in advance.

Last edited by MheAd; 05-17-2010 at 02:04 PM.
 
Old 05-17-2010, 01:53 PM   #2
timetraveler
Member
 
Registered: Apr 2010
Posts: 243
Blog Entries: 2

Rep: Reputation: 31
use warnings;

Can comment out and try.
 
Old 05-17-2010, 01:57 PM   #3
MheAd
Member
 
Registered: Jun 2007
Distribution: Ubuntu 14.04
Posts: 186

Original Poster
Rep: Reputation: 36
Hi,
No effect after I did what you suggested.
 
Old 05-17-2010, 02:04 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MheAd View Post
Hi guys,
I've been reading / trying to learn some Perl lately. I've found the free book, "Learning Perl", quite good and easy to follow most of the times. However, today I was testing one of the scripts in the book and it would only run partly.

Code:
#!/usr/bin/perl 

use warnings;
use strict;

my %inventory;
print "Enter individual items, followed by a new line.\n";
print "Enter a blank line to finish.\n";

while (1) {
        my $item = <STDIN>;
        chomp $item;
        last unless $item;
        $inventory{lc $item}++;
}

open (SORT, "| perl sort.plx") or *SORT = *STDOUT;
select *SORT;

while (my ($item, $quantity) = each %inventory) {
        if ($quantity > 1) {
                $item =~ s/^(\w+)\b/$1s/ unless $item =~ /^\w+s\b/;
        }
        print "$item: $quantity\n";
}
The problem is the open(SORT...) row.
This row is supposed to give option, either to use an additional script as pipe (sort.plx), or STDOUT if the extra script is not found.

It works with the other script present, however, with the sort.plx being absent it simply wont use STDOUT instead but whines to me:

Can't open perl script "sort.plx": No such file or directory

Has the author of the original script done some error writing it? Since I'm a beginner and found most of the stuff confusing in Perl either way (having previously only experience in Bash and some in PHP) - I can't really tell for sure , but from what I can read in documentation - it doesn't appear to be wrong.

As it looks now, the only way for me to use STDOUT is only by removing:
open (SORT, "| perl sort.plx") or *SORT = *STDOUT;
select *SORT;

rows. But then, I use the piping in to that other script as an option.

Any ideas? Thanks in advance.
The whole idea of modifying global entities is wrong/bad. You do not need 'select'. In Perl one can (and should) use lexical variables as file handles. I.e. something like this:

Code:
my $sort_fh;
open ($sort_fh, "| perl sort.plx") or do{$sort_fh = \*STDOUT};

...

   print $sort_fh "$item: $quantity\n";
...
.
 
Old 05-17-2010, 02:13 PM   #5
MheAd
Member
 
Registered: Jun 2007
Distribution: Ubuntu 14.04
Posts: 186

Original Poster
Rep: Reputation: 36
After applying your suggestion, the script did actually manage to print output, correctly too, but the error message still persists (even without use strict / use warnings).

Also, this is the first time I read that lexical variables should be used as standard file handles.

Any other ideas?
 
Old 05-17-2010, 02:59 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MheAd View Post
After applying your suggestion, the script did actually manage to print output, correctly too, but the error message still persists (even without use strict / use warnings).

Also, this is the first time I read that lexical variables should be used as standard file handles.

Any other ideas?
And why do you think the error message shouldn't persist ?

...

If you read 'perldoc -f open', you'll see a number of examples with lexical variable as file handle.
 
Old 05-17-2010, 03:11 PM   #7
MheAd
Member
 
Registered: Jun 2007
Distribution: Ubuntu 14.04
Posts: 186

Original Poster
Rep: Reputation: 36
I've seen few lexical variables in the doc file, yes.
But two books are telling me so far that it's not the standard way to go.
And since I'm trying to learn Perl here, I'm asking the question that teachers hear many times - 'why?' - so I know this for the future reference. Because, you seem to be encouraging the usage.

Last edited by MheAd; 05-17-2010 at 03:15 PM.
 
Old 05-17-2010, 03:18 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MheAd View Post
I've seen few lexical variables in the doc file, yes.
But two books are telling me so far that it's not the standard way to go.
And since I'm trying to learn Perl here, I'm asking the question that teachers hear many times - 'why?' - so I know this for the future reference. Because, you seem to be encouraging the usage.
I really do not care about Perl books. Global vs lexical is a general concern, and the the book authors seem to be copying ancient examples from the Perl 4 era.

So, have you come to a conclusion about the error message ?
 
Old 05-18-2010, 08:41 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You may want to bookmark the official Perl docs here http://perldoc.perl.org/ See also the extra tutorials http://www.perlmonks.org/?node=Tutorials
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl - filehandles always defined? maxfacta Programming 12 11-27-2008 01:07 AM
problem in accessing variables declared in another perl script john83reuben Programming 3 03-21-2008 02:45 AM
Accessing perl array rjcrews Programming 5 11-08-2006 07:16 PM
Perl: Accessing value from DBM file. cramer Programming 2 08-19-2006 05:00 PM
accessing inodes with perl the_uplink2x Programming 0 06-10-2005 10:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration