1753878 Members
7267 Online
108809 Solutions
New Discussion юеВ

Learning PERL

 
hvl_08
New Member

Learning PERL

I wrote the following perl script but during compiling the error: "Global symbol $filename requires explicit package name" pops up and complation fails.
The script is to replace a certain pattern by opening given files of a particular type ( here .html files) #!/usr/bin/perl
use strict;
use warnings;


if ( @ARGV != 3 )
{
print("Arguments not sufficient\n");
exit(1);
}


my $oldname = shift(@ARGV);
my $newname = shift(@ARGV);
$filemaname = shift(@ARGV);
my @readlist;

if ( $filename = ~/\*/)
{
my @filelist= <*.html>;
foreach my $file (@filelist)
{
open(my $in, '<',$file )|| die ("Cannot open $file : $!") ;
while (<$in>)
{
push(@readlist,$_);
}
close($in);


open(my $out,">",$file);
foreach my $change(@readlist)
{
my $a =~ s/$oldname/$newname/g;
print( $out $a);
}
close($out);
}
}
exit(0);
4 REPLIES 4
Patrice Le Guyader
Respected Contributor

Re: Learning PERL

Hello,

just a my before your line :
$filemaname = shift(@ARGV);

like this :

my $filemaname = shift(@ARGV);

Hope this helps
Kenavo.
Pat.
Good judgement comes with experience. Unfortunately, the experience usually comes from bad judgement.
James R. Ferguson
Acclaimed Contributor

Re: Learning PERL

Hi:

The 'strict' pragma is doing exactly what it should do.

You haven't identified the package in which '$filemaname' occurs, nor for that matter '$filename'.

You have a typographical error. Change:

$filemaname = shift(@ARGV);

...to:

my $filename = shift(@ARGV);

...and you syntax errors will vanish.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: Learning PERL

The basic problem is covered, but since you indicate you are learning perl, consider

1) Drop the: if ( @ARGV != 3 )... bit

Instead use:

:
my $filename = shift or die "Missing filename argument";



and uh... in the output loop you probably mean:

foreach my $change(@readlist)
{
$change =~ s/$oldname/$newname/g;
print( $out $change);
}

Personally I would not bother with the @readlist, unless there is more to do.
I would just make in and out loop happen at the same time:

open in
open out
foreach in
{
substitute
print out
}


Cheers,
Hein.










dirk dierickx
Honored Contributor

Re: Learning PERL

also add diagnostics to your list of modules to use, it will give you more output on errors that already go a long way to get you on the right track regarding the mistake you made.

http://perldoc.perl.org/diagnostics.html