Reputation: 25
I'm learning Perl formatting by following the tutorial at Perl - Formats, but when I type their example into my IDE:
format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<<
$name $age
@#####.##
$salary
===================================
.
select(STDOUT);
$~ = EMPLOYEE;
@n = ("Ali", "Raza", "Jaffer");
@a = (20,30, 40);
@s = (2000.00, 2500.00, 4000.000);
$i = 0;
foreach (@n) {
$name = $_;
$age = $a[$i];
$salary = $s[$i++];
write;
}
I get the error:
Scalar found where operator expected at .\qb.pl line 7, near "$name $age"
(Missing operator before $age?)
syntax error at .\qb.pl line 7, near "$name $age"
Execution of .\qb.pl aborted due to compilation errors.
I'm using Perl 5, version 30, Subversion 2 (v5.30.2) built for MSWin32-x64-multi-thread.
Upvotes: 1
Views: 243
Reputation: 6798
Perhaps the following code sample is easier to read. The output is also easier to comprehend at a quick glance:
use strict;
use warnings;
my($name, $age, $salary);
while(<DATA>) {
($name, $age, $salary) = split;
write;
}
format STDOUT_TOP =
Employee Age Salary
----------------------------------------
.
format STDOUT =
@<<<<<<<<<<<<<<<<<<<<<< @<< @#####.##
$name, $age, $salary
.
__DATA__
Ali 20 2000.00
Raza 30 2500.00
Jaffer 40 4000.00
Employee Age Salary
----------------------------------------
Ali 20 2000.00
Raza 30 2500.00
Jaffer 40 4000.00
There are many ways to provide input data. One of them is a hash to keep related data.
use strict;
use warnings;
my %employees = (
Ali => { age => 20, salary => 2000.00},
Raza => { age => 30, salary => 2500.00},
Jaffer => { age => 40, salary => 4000.00}
);
my($name, $age, $salary);
for $name (sort keys %employees) {
($age, $salary) = @{$employees{$name}}{qw/age salary/};
write;
}
format STDOUT_TOP =
Employee Age Salary
----------------------------------------
.
format STDOUT =
@<<<<<<<<<<<<<<<<<<<<<< @<< @#####.##
$name, $age, $salary
.
Employee Age Salary
----------------------------------------
Ali 20 2000.00
Jaffer 40 4000.00
Raza 30 2500.00
Upvotes: 1
Reputation: 62037
When I run your code on 5.24, I see this warning message:
Use of comma-less variable list is deprecated at...
which points to the $name $age
line as well.
When I enable diagnostics, I get this explanation:
(D deprecated) The values you give to a format should be
separated by commas, not just aligned on a line.
When I add the comma as follows:
$name,$age
the warning goes away, and I get this output:
===================================
Ali 20
2000.00
===================================
===================================
Raza 30
2500.00
===================================
===================================
Jaffer 40
4000.00
===================================
This warning became an error in 5.28, according to perl5280delta.
Upvotes: 3
Reputation: 69224
Your first mistake was in thinking that formats were a useful tool that would solve your problem. Formats have been largely ignored for almost as long as I've been using Perl. You'll probably find that using Perl6::Form is a better approach.
Your second mistake was in thinking that Tutorials Point was a good place to get information about anything. The tutorials there are written by people who seem to know next to nothing about their subject and (as you have seen here) the examples are riddled with typos that makes them next to useless.
If you're determined to use Perl formats, then the perlform manual page would be the best place to start.
Upvotes: 3