ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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";
...
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.
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.
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'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 ?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.