Newbie question

P

Petterson Mikael

Hi,

I am trying to run the following perl script but I cannot
get it to compile. I get the following error:


"my" variable $msg masks earlier declaration in same scope at
run_dtest.pl line 34.
syntax error at run_dtest.pl line 33, near "sub printWARNING "
Can't use global @_ in "my" at run_dtest.pl line 34, near "=$_"
syntax error at run_dtest.pl line 38, near "}"
Execution of run_dtest.pl aborted due to compilation errors.

All help is very much appreciated.

//Mikael



####################### SCRIPT #######################

#!/usr/local/bin/perl -w

# turn on perl's safety features
use strict;
#use warning;

# load our modules

my $msg;
my $current_dir = `pwd`;
my $log="dtest.log";
print "Current directory is $current_dir";

# Main

prepare


######################################################################
### SUBROUTINES
######################################################################

######################################################################
### printWARNING to stdout and $log
######################################################################
sub printWARNING {
my $msg=$_[0];
# make STDOUT go to log file, plus original STDOUT
open (STDOUT, "| tee $log") or die "Teeing off: $!\n";
print "WARNING: $msg \n" or die "Writing: $!\n";
}

sub prepare () {
rmdir "$current_dir/RBSSW*" or printWARNING("No RBSSW* found the
search directory.");





}
 
G

Gunnar Hjalmarsson

Petterson said:
I am trying to run the following perl script but I cannot
get it to compile. I get the following error:

"my" variable $msg masks earlier declaration in same scope at
run_dtest.pl line 34.
syntax error at run_dtest.pl line 33, near "sub printWARNING "
Can't use global @_ in "my" at run_dtest.pl line 34, near "=$_"
syntax error at run_dtest.pl line 38, near "}"
Execution of run_dtest.pl aborted due to compilation errors.

Take a look at the statement right *before* line 33, and you'll find the
explanation.
 
A

A. Sinan Unur

I am trying to run the following perl script but I cannot
get it to compile. I get the following error:

"my" variable $msg masks earlier declaration in same scope at
run_dtest.pl line 34.
syntax error at run_dtest.pl line 33, near "sub printWARNING "
Can't use global @_ in "my" at run_dtest.pl line 34, near "=$_"
syntax error at run_dtest.pl line 38, near "}"
Execution of run_dtest.pl aborted due to compilation errors. ....
####################### SCRIPT #######################

#!/usr/local/bin/perl -w
#!/usr/local/bin/perl

# turn on perl's safety features
use strict;
#use warning;

use warnings;

....
# Main

prepare

What is this?
 
A

Arndt Jonasson

Petterson Mikael said:
I am trying to run the following perl script but I cannot
get it to compile. I get the following error:


"my" variable $msg masks earlier declaration in same scope at
run_dtest.pl line 34.
syntax error at run_dtest.pl line 33, near "sub printWARNING "
Can't use global @_ in "my" at run_dtest.pl line 34, near "=$_"
syntax error at run_dtest.pl line 38, near "}"
Execution of run_dtest.pl aborted due to compilation errors.
[...]

I suggest using just "perl -c" to check the syntax, and stripping off
things from your script until the error messages change or
disappear. By then it may be obvious what the syntax error is.
 
A

Anno Siegel

Ken said:
Try this instead:

prepare();

You can't call subroutines without the parentheses.

That's wrong. Parameterless subs are called without parentheses all
the time. The only requisite is that the sub be declared at compile
time. That means it must be defined, or at least predeclared, before
its first use.
You also need to
make sure all expressions end in a semicolon.

That's not necessary either. The last statement in a block doesn't
need a semicolon, though it is usually written unless the block ends
on the same line.

Anno
 
K

Ken

Dave said:
Try this instead:

prepare();

You can't call subroutines without the parentheses.


Yes you can, as long as the sub has been defined before the call:

[davew@localhost tmp] $ perl
#!/usr/bin/perl
use strict;
use warnings;

sub wotsit {
print "here\n";
}

wotsit;
__END__
here
[davew@localhost tmp] $

Define the sub after the call, however, and it fails:

[davew@localhost tmp] $ perl
#!/usr/bin/perl
use strict;
use warnings;

wotsit;

sub wotsit {
print "here\n";
}
__END__
Bareword "wotsit" not allowed while "strict subs" in use at - line 5.
Execution of - aborted due to compilation errors.
[davew@localhost tmp] $

Cool I didn't know that. Still, probably not a good habit to get into.

- Ken
 
K

Ken

Tad said:

I just think it can get confusing. When you put the empty (), you know
right away that it is a sub. Maybe it's just my own personal habit
though. Also, I have always put subs at the bottom of my script which is
probably why I've never tried calling a sub as a bare word.

So it's bad habit only because it could be confusing to someone else
reading your script. And because it looks weird ;).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top